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!