Can't POST to a webserver from an iPhone using a PHP script
Well I am pretty new to both iphone and PHP development, and am trying to simply post an image locally on to my webserver (i have setup the webserver already). From some reason, my P开发者_开发知识库HP script is being executed, though the file is not being saved.
I am sure that this is the most basic problem here, but I just cant seem to find the problem. Would REALLY appreciate you guys' help. Thanks!
My objective-c code:
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"13.png"];
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
NSString *urlString = @"http://localhost/uploader.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\";filename=\"myfile.jpg\"\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSLog(@"%@",[[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding]);
My PHP script:
<?php
$target = "./upload/";
$target = $target.basename( $_FILES['userfile']['name']) ;
$check = getimagesize($_FILES['userfile']['tmp_name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], './upload/')) {
echo "YES";
}
else
echo "NO";
?>
Those are the values that I get when printing these variables in PHP:
print_r($target);
./upload/myfile.jpg
print_r($check[0]);
320
print_r($_REQUEST);
Array
(
)
print_r($_FILES[userfile]);
Array
(
[name] => myfile.jpg
[type] => application/octet-stream
[tmp_name] => /private/var/tmp/phpWJE4UC
[error] => 0
[size] => 8829
)
print_r($_FILES[name]):
(empty)
print_r($_FILES[type]);
(empty)
print_r($_FILES[tmp_name]);
(empty)
print_r($_FILES[error]);
(empty)
print_r($_FILES[size]);
(empty)
print_r($_FILES);
Array
(
[userfile] => Array
(
[name] => myfile.jpg
[type] => application/octet-stream
[tmp_name] => /private/var/tmp/phpMvJ4oR
[error] => 0
[size] => 8829
)
)
Your move call is incorrect. You must specify a filename for the destination. You've only provided a directory, so the call fails. PHP will not create a filename for you, you must specify it yourself.
if (move_uploaded_file($_FILES['userfile']['tmp_name'], './upload/name_of_file_to_write')) {
^^^^^^^^^^^^^^^^^^^^^
精彩评论