doumengwei0138 2011-12-14 03:36
浏览 32
已采纳

iOS应用程序的PHP is_uploaded_file失败

I'm writing the PHP/webserver side of an image upload for an iOS app. Using an html file I can upload an image to my script just fine. I've even written a Perl LWP script to post an image with no problems.

When the iOS app uploads an image it fails when I call is_uploaded_file. Sending back the $_FILES var in the json response show us what we expect for a file upload. Also I do a file_exists on the tmp_name and that fails as well.

Is there anything else I can look at in the request to determine what is going wrong? I'd like to be able to indicate what's wrong with the post request.

Here's the block of code where it stop processing the image upload:

    //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条回答 默认 最新

  • drmcm84800 2011-12-14 04:47
    关注

    As far as i know you cannot upload a image or photo from iOS app directly using PHP scripts.

    You have to send 64 bit encode from the iOS app to the script responsible for the file upload as a simple POST. Then that script will firstly decode your photo's string and then PHP script will create the image from the string only to upload.

    Code will be something like:

    $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
      }
    }
    

    Hope this will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入