这是我尝试使用媒体捕获 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 进行测试。
- 这是我的 iPhone 的功能:
我选择选择文件选项,它会打开菜单以拍照或选择现有图像。我选择拍照选项,我的相机打开并拍照,然后我选择使用选项并提交表单。在 processUpTests.php 上,我收到错误消息“上传文件时出现问题,请重试!”。我正在尝试 print_r $_files 数组,它什么也不输出。似乎没有文件被发送到服务器。此外,没有上传延迟,脚本会立即输出我的错误消息,这让我相信没有文件被发送。
- 其他结论:在我的测试期间,我的一个脚本会上传文件并且它按预期工作,只有一次,然后如果我尝试上传另一个文件,它会简单地跳过上传,并输出我拍摄的第一张图片。脚本与下面的并没有太大的不同。如果我从服务器中删除图像,它会再次工作。自然地,这让我相信我可以上传到服务器上的不同文件夹,将文件复制并重命名到我的上传目录并取消链接原始上传。我错了,没有区别。归结为一件事,我可以使用脚本拍照并上传一次,如果我想上传另一张照片,我必须删除之前的一张。
我不确定这是否对任何人有帮助,但我认为这可能是相关的。再说一次,这可能只是代表我的语法错误。