这是我尝试使用媒体捕获API上传iPhone的html表单:
<form action="processUpTests.php" method="post" name="capture" id="capture" enctype="multipart/form-data">
<input type="file" accept="image/*" name="ImageFile" id="ImageFile" onchange="javascript:stabEagle();"/>
<input type="submit" id="SubmitButton" class="hiddenForm" />
</form>
通过此stabEagle()提交表单:
function stabEagle(){
$('#SubmitButton').click();
}
这是用于实现目标的processUpTests.php:
if(isset($_POST))
{
$DestinationDirectory = 'upload/'; //Upload Directory
// check $_FILES['ImageFile'] array is not empty
// "is_uploaded_file" Tells whether the file was uploaded via HTTP POST
if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name']))
{
die('Something went wrong with Upload!'); // output error when above checks fail.
}
// Random number for both file, will be added after image name
$RandomNumber = rand(0, 9999999999);
$ImageType = $_FILES['ImageFile']['type']; //Obtain file type, returns "image/png", image/jpeg, text/plain etc.
//Let's use $ImageType variable to check whether uploaded file is supported.
//We use PHP SWITCH statement to check valid image format, PHP SWITCH is similar to IF/ELSE statements
//suitable if we want to compare the a variable with many different values
switch(strtolower($ImageType))
{
case 'image/png':
$type = "good";
break;
case 'image/gif':
$type = "good";
break;
case 'image/jpeg':
case 'image/pjpeg':
$type = "good";
break;
default:
die('Unsupported File!'); //output error and exit
}
$target_path = "upload/";
//naming loop---------------------------------//
$i = 9; //how many characters
$prefix =""; //declares prefix var as a string
$letters = ""; //declares letters var as string
//execute loop
for($e = 0; $e < $i; $e++){
//random letter
if($odd = $e%2){
$prefix.= chr(rand(97,122));
}
//random number
$prefix.= rand(0,9);
}
$target_path = $target_path .$prefix. basename( $_FILES['ImageFile']['name']);
if(move_uploaded_file($_FILES['ImageFile']['tmp_name'], $target_path)) {
echo "<img id='userImage' src='".$target_path."' style='max-width:310px'/>";
} else{
echo "There was an error uploading the file, please try again!<br/>";
echo "<pre>".print_r($_FILES)."</pre>";
}
当我使用计算机上传照片时,它可以正常工作。当我使用iPhone时,它不起作用。我有一台带有最新操作系统的iPhone 5进行测试。
我选择了选择文件选项,它会打开菜单以拍摄照片或选择现有图像。我选择了拍照选项,我的相机打开并拍照,然后选择使用选项并提交表格。在processUpTests.php上,我收到错误消息“上传文件时出现问题,请再试一次!”。我试图print_r $ _files数组,它什么也没输出。似乎没有文件被发送到服务器。此外,没有上传延迟,脚本立即输出我的错误消息,这使我相信没有文件被发送。
我不确定这对任何人都有帮助,但我认为这可能是相关的。然后,它可能只是代表我的语法错误。