I'm getting an error uploading a file to a http server. Here is the code that I'm using:
UIImage * ourImage = [UIImage imageNamed:@"tux.png"];
NSData * imageData = UIImageJPEGRepresentation(ourImage, 1.0);
NSString *urlString = @"http://www.site.com/imageupload.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"userfile\"; filename=\".png\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSString *param1 = @"parameter text";
[body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter1\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];
// Another text parameter
NSString *param2 = @"Parameter 2 text";
[body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter2\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param2] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
and this is the PHP file that I have in public_html/ directory in the Server. The file name is imageupload.php. I am trying to upload an Image file via HTTP not using any log in name or password. I am not to familiar with PHP and really need some help. Thanks.
<?php
$uploaddir = '/public_html/upload';
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploadDir . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded.
";
} else {
echo "Temp file not uploaded.
";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "http://www.iroboticshowoff.com/dir/{$file}" . "
" . $_FILES['userfile']
['size'] . "
" . $_FILES['userfile']['type'] ;
}
?>
and this is the error that I'm getting back from the server when I execute the code:
2012-07-31 10:04:24.576 FTP6[3600:f803]
PHP Error Message
Parse error: syntax error, unexpected T_STRING in /home/a5054180/public_html/imageupload.php on line 2
Free Web Hosting
Plus other errors. Can anyone try it out and let me know what I can do to fix it. I would appreciate it thanks a lot.