使用PHP从HTTP POST获取图像 [英] Get image from HTTP POST with PHP
问题描述
file_get_contents $ c $它不喜欢那样。我发送的图像也来自iPhone,所以我的POST请求看起来像这样。 NSMutableData * postBody = [NSMutableData data ]。
[postBody appendData:[[NSString stringWithFormat:@ - %@ \r\\\
,stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@Content-Disposition:form-data; name = \media \; filename = \dummy.jpg \\r\\\
dataUsingEncoding:NSUTF8StringEncoding ]];
[postBody appendData:[@Content-Type:image / jpeg\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@Content-Transfer-Encoding:binary \r\\\
\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[@\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@ - %@ - \r\\\
,stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
请求的其余部分已写入,但我并不想发布所有请求。
不过,无论如何,这就是我发送请求的方式,并且在PHP服务器端,
<?php
echoSome Message;
$ nameValue = $ _POST ['name'];
回声< br />;
echo $ nameValue;
$ otherValue = $ _POST ['other'];
回声< br />;
echo $ otherValue;
file_get_contents('dummy.jpg');
//或者我试过这个
$ imageData = $ _POST ['media'];
?>
我似乎无法抓住图像。这两个尝试获取图像失败,我不知道为什么。有什么建议吗?
编辑: 我添加了print语句 print_r($ _ FILES)
,看看我会得到什么,并返回下列内容。
(
[media] => Array
(
[name] => dummy.jpg
[type] =>
[tmp_name] => ;
[错误] => 1
[size] => 0
)
)
所以它看起来像是获取图像,至少是它的名字,但由于某种原因,它没有得到实际的数据,并且正在抛出一些错误。我想这可能是在iPhone的一面,如果它不发送数据,我的PHP服务器发现错误。我不确定,任何人都会遇到这样的事情吗?
编辑2: b
$ b <我终于得到了一切工作。事实证明,一切都格式正确我只是有一个文件图像大小上传限制问题。我发送的图像大约是8MB,显然我们的服务器无法处理,所以我只是降低了jpeg的质量,而这个问题就解决了。然后还有一些权限问题,没有一点 sudo chmod
无法修复。谢谢大家的建议和帮助,我希望我可以接受多个答案! 解决方案
<1>错误代码1表示您尝试上传的图片太大了。
http://php.net/manual/en/features .file-upload.errors.php
检查您在php.ini中的upload_max_filesize设置。
我看到你正在试图继续使用$ _FILES ['media'] ['name'],但实际上传的文件可以通过$ _FILES ['media'] ['tmp_name']中给出的路径获得。 p>
为确保正确处理您的上传,请参阅php-docu:
http://php.net/manual/en/features.file-upload.post-method.php
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:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"media\"; filename=\"dummy.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", 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!
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
这篇关于使用PHP从HTTP POST获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!