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

问题描述 投票:1回答:1

我正在尝试创建一个HTML和PHP脚本,以便从用户的手机上将图像上传到网络服务器。它工作得很好,它会提示用户从他们的图库中选择一个图像,或用他们的相机捕获图像。当我拍摄照片时,它会成功填写信息

“Foo.Jpg”

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

“警告getimagesize()文件名不能为空....抱歉,上传文件时出错。”

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

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.";
       }
    }
}

?>

到目前为止,我要感谢大家的善意和耐心的帮助,我感谢任何答案和指导。


UPDATE:

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

这是来自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.

Android 6.0.1 chrome:

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

它似乎永远不会发送图像类型或文件大小,只有使用移动设备时的名称。有任何想法吗?

php html mobile image-uploading
1个回答
1
投票

这似乎是一个CORS问题。

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

请参阅您的wamp服务器的日志文件以获取详细的错误信息...

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

© www.soinside.com 2019 - 2024. All rights reserved.