dongxie9448 2012-08-08 19:21
浏览 40
已采纳

使用PHP从HTTP POST获取图像

So I am sending an image over the internet using HTTP POST and sending it to a php script on my server. I want to be able to take that image and save it. I am doing this fine with text, but when it comes to other content, such as an image, it is error-ing saying it can not find the variable, and when I try file_get_contents, it does not like that either. I am sending the image from an iPhone as well, so my POST Request looks like this.

NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@
", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"media\"; filename=\"dummy.jpg\"
" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg
" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary

" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[@"
" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@--
", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];

The rest of the request is written, but I do not really feel like posting all of it.

But regardless, that is how I am sending the request, and on the php server side,

<?php
    echo "Some Message";
    $nameValue =  $_POST['name'];
    echo "<br/>";
    echo $nameValue;
    $otherValue =  $_POST['other'];
    echo "<br/>";
    echo $otherValue;
    file_get_contents('dummy.jpg');
    // or I tried this
    $imageData = $_POST['media'];
?>

I can not seem to be able to grab the image. Both attempts of getting the image failed and I do not know why. Any suggestions?

EDIT:

I added the print statement print_r($_FILES) to see what I would get and it returned the following.

(
    [media] => Array
        (
            [name] => dummy.jpg
            [type] => 
            [tmp_name] => 
            [error] => 1
            [size] => 0
        )
)

So it seems as if it is getting the image, at least the name of it, but for some reason it is not getting the actual data and is throwing some error. I imagine this is probably on the iPhone side of things if it is not sending the data and my PHP server is seeing an error. I am unsure though, anyone ever experience something like this?

EDIT 2:

I finally got everything working. It turns out that everything was formatted properly I was just having a file image size upload limit issue. The images I was sending were around 8MB and apparently our server can not handle that, so I simply downed the quality on the jpeg, and that issue was solved. Then there were some permissions issues, as well, nothing a little bit of sudo chmod can't fix. Thanks everyone for your suggestions and help, I wish I could accept more than one answer!

  • 写回答

3条回答 默认 最新

  • duan1989643 2012-08-09 14:14
    关注

    The error code "1" means that the image you tried to upload is too big. http://php.net/manual/en/features.file-upload.errors.php

    Check your upload_max_filesize setting in php.ini.

    I saw that your are trying to continue with $_FILES['media']['name'], but the actually uploaded file is available by the path given in $_FILES['media']['tmp_name'].

    To be sure you handle your upload correctly refer to the php-docu: http://php.net/manual/en/features.file-upload.post-method.php

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度