upload data from an iphone app to a web server with php
with almost no knowledge in PHP whatsoever, I am trying to send some data (NSData) from my iphone app to my web server (locally for now) but without success (The PHP uploader script is being called since I am getting its error message). could anyone please help me understand what am I doing wrong here?
My iphone app at this point includes only the code below in the ViewDidLoad of my (one and only) viewControll开发者_开发问答er, which I have combined from some of the conversations around here:
- (void)viewDidLoad
{
[super viewDidLoad];
NSData *data;
const char *bytestring = "black dog";
data = [NSMutableData dataWithBytes:bytestring length:strlen(bytestring)];
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSString *urlString = @"http://localhost/uploader1.php";
NSString *filename = @"filename";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
I have also tried the following PHP uploader script:
<?php
$target = "./upload/";
$target = $target.basename( $_FILES['userfile']['name']) ;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "SUCCESS";
}
?>
What am I doing wrong and how come I cant post this data? Again, the PHP script is getting called cause I get the error message: Wrong file type...
Any help would be much appreciated.
Thanks!
In your Objective-C code you are sending the file like this:
name=\"userfile\"; filename=\"%@.jpg\"\r\n",
so, in the PHP side, you should access $_FILES["userfile"]
, instead of $_FILE["file"]
or $_FILE["uploaded"]
, to get the correct information about your upload.
If you are still experiencing problems, first suggestion is putting some traces server-side:
print_r($_REQUEST);
will give you a list of all the $_POST/$_GET/cookies input parameters, so you can better know what is happening. Furthermore, I would suggest also tracing:
print_r($_FILES);
it could be that the ObjC side is sending wrong data, or that wrong data is read on the server side.
EDIT: the error you are getting:
"Wrong file type..."
depends on the fact that you are trying to calculate the size of the uploaded file as if it were an image:
$check = getimagesize($_FILES['userfile']['tmp_name']);
if($check[0]>0 && $check[1]>0 && $check['mime']=='image/jpg'){
while in fact you are sending a simple string as file content:
const char *bytestring = "black dog";
either you remove the check, or you upload a real file.
EDIT:
Use these lines:
error_reporting(E_ALL);
ini_set("display_errors", 1);
to make error reporting more verbose, so you possibly know why it is failing...
Did you try ASIHTTPRequest? http://allseeing-i.com/ASIHTTPRequest/
For target path dont use HTTP, use the method below.
Instead of doing file_put_contents(***WebSiteURL***...)
you need to use the server path to /cache/lang/file.php
(e.g. /home/content/site/folders/filename.php).
You cannot open a file over HTTP and expect it to be written. Instead you need to open it using the local path.
精彩评论