iOS应用程序的PHP is_uploaded_file失败

时间:2011-12-14 03:36:32

标签: php ios file-upload

我正在为iOS应用编写图像上传的PHP / webserver端。使用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;
    }

2 个答案:

答案 0 :(得分:1)

据我所知,您无法使用PHP脚本直接从iOS应用程序上传图像或照片。

您必须将iOS应用程序中的64位编码作为简单的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
  }
}

希望这会奏效。

答案 1 :(得分:1)

以下链接是一个非常好的NSData base64类别。 如果您不熟悉Obj-C中的类别,它们基本上是我们熟悉并喜欢的库存对象,有一些附加的方法(在这种情况下,base64编码/解码)。 这个类很容易进入现有项目。 享受。

http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html