gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:16:44 26 4
gpt4 key购买 nike

我正在尝试创建一个 HTML 和 PHP 脚本,以将图像从用户的手机上传到网络服务器。它运作良好,因为它会提示用户从他们的画廊中选择图像,或者用他们的相机捕捉图像。当我抓拍图片时,它成功填写了信息

"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 以及运行 6.0.1 Marshmallow 和 chrome 浏览器的安卓设备。转储文件确实显示了一些信息。

这是来自 ipad 的 echo :

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.

来自计算机的工作 echo :

    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

关于php - 文件上传适用于桌面,但不适用于使用相机的移动设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40391328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com