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!
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
First you must output the result of file_get_contents
(1), then you need to tell the browser that you're serving up an image instead of standard HTML (2).
echo file_get_contents('dummy.jpg'); // 1
header("Content-Type: image/jpg"); // 2
I just skimmed the surface for the header
function; you'll want to add a few more headers in a real-world case. Check out:
Check the $_FILES array on the PHP side and see if your image is in there...
Do print_r($_FILES);