我正在为 iOS 应用编写图片上传的 PHP/网络服务器端。使用 html 文件,我可以将图像上传到我的脚本中。我什至编写了一个 Perl LWP 脚本来毫无问题地发布图像。
当 iOS 应用程序上传图像时,调用 is_uploaded_file 时失败。在 json 响应中发回 $_FILES var 向我们展示了我们对文件上传的期望。我还对 tmp_name 执行了 file_exists,但同样失败了。
我是否可以在请求中查看其他任何内容以确定出了什么问题?我希望能够指出发布请求有什么问题。
这是它停止处理图片上传的代码块:
//Check for valid upload
if(!is_uploaded_file($_FILES['photo']['tmp_name'])) {
$this->errors['upload_2'] = $photoErrorString;
$this->errors['files'] = $_FILES;
$this->errors['image_uploaded'] = file_exists($_FILES['photo']['tmp_name']);
error_log("uploadPhoto: upload_2");
return false;
}
最佳答案
据我所知,您不能直接使用 PHP 脚本从 iOS 应用程序上传图像或照片。
您必须将 64 位编码从 iOS 应用发送到负责文件上传的脚本,作为一个简单的 POST。然后该脚本将首先解码您的照片字符串,然后 PHP 脚本将从该字符串创建图像,仅供上传。
代码会是这样的:
$filedb = $path_to_dir.$file_name_of_the_photo;
$profile_pic = $_POST['profile_pic'];
$profile_pic= base64_decode($profile_pic);
if(($img = @imagecreatefromstring($profile_pic)) != FALSE)
{
if(imagepng($img,$filedb))//will create image and save it to the path from the filedb with the name in variable file_name_of_the_photo
{
imagedestroy($img);// distroy the string of the image after successful upload
// do other updates to database if required
}
else //cannot create imagepng Error
{
//do what required
}
}
希望这会奏效。
关于iOS 应用程序的 PHP is_uploaded_file 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499223/