AWS Elastic Beanstalk 文件上传不起作用


AWS Elastic Beanstalk file upload not working

我们使用 AWS Elastic Beanstalk 来托管 PHP 应用程序,其中包括无法正常工作的文件上传工具。我们有 php.ini将tmp_upload_dir设置为/tmp,但它仍然不起作用。

我们刚刚从另一台服务器移动了站点,那里的一切运行良好,但 EB 似乎不想让我们上传文件。

下面是我们正在使用的代码示例:

$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',
    );
  }

我遇到了同样的问题,发现我们需要两样东西,这两样东西都放在 .htaccess 文件中:

 php_value upload_tmp_dir "/tmp"
 php_value upload_max_filesize 10M

上传目录必须由 Web 服务器拥有,例如"webapp"或"守护程序",或者可由该帐户写入。此外,最大文件大小必须适合您的上传。

就我而言,默认情况下上传限制为 2M,我的文件为 4M。这导致一个空的 $_FILES 数组。