文件上传适用于桌面,但不适用于使用相机的移动设备

File upload works on desktop, but not on mobile using camera

我正在尝试创建一个 HTML 和 PHP 脚本来将图像从用户的 phone 上传到网络服务器。它运作良好,因为它会提示用户 select 从他们的图库中获取图像,或者使用他们的相机拍摄图像。当我拍照时,它成功填写了信息

"foo.jpg"

但是当我将数据传递到我的 PHP 脚本时,它给我错误

"Warning getimagesize() filename cannot be empty.... Sorry, there was an error uploading your file."

让我感到困惑的是,我的表单中的文件名不是空的。此外,当我使用台式计算机提交表单时(它只是提示上传文件),它可以正常工作。

HTML:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" capture="camera" accept="image/*" name="imageFileToUpload">  
    <label for="rma">RMA: </label>
    <input type="text" name="rma">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

PHP:

<?php
$rma = $_POST['rma'];
echo "RMA: " . $rma. "<br>";
$target_dir = "uploads/". $rma;

if (file_exists($target_dir)==false) 
{
    if(mkdir ($target_dir))
    {
        echo "folder created at: " .$target_dir . "<br>";
    }
    else
    {
        echo "failed to create folder " . $target_dir. "<br>";
    }
}

$target_file = $target_dir ."/" .basename($_FILES["imageFileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) 
{
    $check = getimagesize($_FILES["imageFileToUpload"]["tmp_name"]);
    if($check !== false) 
    {
          //echo "File is an image - " . $check["mime"] . ".";
          $uploadOk = 1;
    } 

    else 
    {
      echo "File is not an image.<br>";
          $uploadOk = 0;
    }

    // Check if file already exists
    if (file_exists($target_file)) 
    {
       echo "Sorry, file already exists.<br>";
        $uploadOk = 0;
    }

    // if everything is ok, try to upload file
    else 
    {
       if (move_uploaded_file($_FILES["imageFileToUpload"]["tmp_name"], $target_file)) 
       {
          echo "The file ". basename( $_FILES["imageFileToUpload"]["name"]). " has been uploaded.<br>";
       } 

       else 
       {
             echo "Sorry, there was an error uploading your file.";
       }
    }
}

?>

我要感谢大家迄今为止的友善和耐心帮助,我感谢任何答案和指导。


更新:

我正在使用带有 Safari 的 Ipad 以及 android 设备 运行 6.0.1 Marshmallow 和 chrome 浏览器。转储文件确实显示了一些信息。

这是来自 ipad 的回显:

RMA: 2468
C:\wamp64\www\upload.php:5:
array (size=5)
  'name' => string 'image.jpg' (length=9)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 1
  'size' => int 0
folder created at: uploads/2468

( ! ) Warning: getimagesize(): Filename cannot be empty in C:\wamp64\www\upload.php on line 31
Call Stack
#   Time    Memory  Function    Location
1   0.0006  376976  {main}( )   ...\upload.php:0
2   0.0011  377176  createImageFile( )  ...\upload.php:20
3   0.0012  377208  getimagesize ( )    ...\upload.php:31
File is not an image.
Sorry, there was an error uploading your file.

Android6.0.1chrome:

RMA: 1996
C:\wamp64\www\upload.php:5:
array (size=5)
  'name' => string '20160322_125659.jpg' (length=19)
  'type' => string '' (length=0)
  'tmp_name' => string '' (length=0)
  'error' => int 1
  'size' => int 0
folder created at: uploads/1996

( ! ) Warning: getimagesize(): Filename cannot be empty in C:\wamp64\www\upload.php on line 30
Call Stack
#   Time    Memory  Function    Location
1   0.0006  377880  {main}( )   ...\upload.php:0
2   0.0012  378096  createImageFile( )  ...\upload.php:20
3   0.0012  378128  getimagesize ( )    ...\upload.php:30
File is not an image.
Sorry, there was an error uploading your file.

来自计算机的工作回显:

    RMA: 1234
C:\wamp64\www\upload.php:5:
array (size=5)
  'name' => string '58832_300x300.jpg' (length=17)
  'type' => string 'image/jpeg' (length=10)
  'tmp_name' => string 'C:\wamp64\tmp\phpF5F3.tmp' (length=25)
  'error' => int 0
  'size' => int 3801
The file 58832_300x300.jpg has been uploaded.
New record created successfully

它似乎从不发送图像类型或文件大小,仅在使用移动设备时发送名称。有什么想法吗?

好像是CORS的问题。

尝试从其他位置上传时出错。

查看 wamp 服务器的日志文件以获取详细的错误信息...

有关该机制的更多信息,请参阅:CORS wiki