Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Elastic Beanstalk file upload not working

We use AWS Elastic Beanstalk to host PHP applications which include file upload facilities which aren't working. We have php.ini set the tmp_upload_dir to /tmp but it still doesn't work.

We've just moved the site from another server, everything was working perfectly there, but EB doesn't seem to want to let us upload files.

Here's an example of the code we are using:

$imagePath = "/tmp/";

$allowedExts = array("gif", "jpeg", "jpg", "png", "GIF", "JPEG", "JPG", "PNG");
$temp = explode(".", $_FILES["img"]["name"]);
$extension = end($temp);

if ( in_array($extension, $allowedExts))
  {
  if ($_FILES["img"]["error"] > 0)
    {
         $response = array(
            "status" => 'error',
            "message" => 'ERROR Return Code: '. $_FILES["img"]["error"],
        );
        echo "Return Code: " . $_FILES["img"]["error"] . "<br>";
    }
  else
    {

      $filename = $_FILES["img"]["tmp_name"];
      list($width, $height) = getimagesize( $filename );

      move_uploaded_file($filename, $imagePath . $_FILES["img"]["name"]);

      $response = array(
        "status" => 'success',
        "url" => $imagePath.$_FILES["img"]["name"],
        "width" => $width,
        "height" => $height
      );

    }
  }
else
  {
   $response = array(
        "status" => 'error',
        "message" => 'something went wrong',
    );
  }
like image 822
flyersun Avatar asked Dec 06 '25 09:12

flyersun


2 Answers

I had the same problem, and found that we needed two things, both of which were put into the .htaccess file:

 php_value upload_tmp_dir "/tmp"
 php_value upload_max_filesize 10M

The upload directory must be owned by the web server, e.g. "webapp", or "daemon", or writable by that account. In addition, the max filesize must accommodate your uploads.

In my case, the upload limit was 2M by default, and my files were 4M. This resulted in an empty $_FILES array.

like image 120
JDA3 Avatar answered Dec 08 '25 02:12

JDA3


I had the same problem, and found that we needed two things, both of which were put into the .htaccess file:

 php_value upload_tmp_dir "/tmp"
 php_value upload_max_filesize 10M

The upload directory must be owned by the web server, e.g. "webapp", or "daemon", or writable by that account. In addition, the max filesize must accommodate your uploads.

In my case, the upload limit was 2M by default, and my files were 4M. This resulted in an empty $_FILES array.

like image 21
JDA3 Avatar answered Dec 08 '25 00:12

JDA3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!