我正尝试创建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棉花糖的Android设备和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.
的Android 6.0.1铬:
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
好像它从来没有发送图像类型或文件大小,只有使用移动设备时的名称。有任何想法吗?
谢谢你的标题编辑弗雷德,我不能完全肯定上最好的措辞。 – mberna
您是否试图转储'$ _FILES [“imageFileToUpload]'?数据集在那里?并辅以手机上的操作系统? – Armen
你好!这似乎是一个CORS问题。尝试从其他位置上传时出现错误。请参阅您的wamp服务器的日志文件以获取详细的错误信息。 –