Reputation: 609
I don't know if the problem is in my PHP code or on the objective-c side. I don't understand this very well so would appreciate any help getting files to upload from the iPad app I am creating to a mysql database. Right now, only the description of the file is getting posted into the database, but the blob/file is not. Any help would be much appreciated!
Here is my PHP code:
<?php
$username = "person";
$password = "xxxxxxx";
$database = "database";
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");
$file = $_FILES['file'];
$name = $file['tmp_name'];
$testpage = file_get_contents($name);
$testpage = mysql_real_escape_string($testpage);
mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')");
mysql_close();
?>
Here is my objective-c code, packaging it into an HTTP packet
NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[post_dict setObject:@"test_value" forKey:@"test_key"];
[post_dict setObject:[NSURL fileURLWithPath:[pdfUrl absoluteString]] forKey:@"file"];
NSData* regData = [self generateFormData:post_dict];
[post_dict release];
NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://myserver/upload.php"]];
[post addValue: @"multipart/form-data; boundary=_insert_some_boundary_here_" forHTTPHeaderField: @"Content-Type"];
[post setHTTPMethod: @"POST"];
[post setHTTPBody:regData];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error];
NSLog(@"%@", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
Thanks, Rossi
Upvotes: 0
Views: 134
Reputation: 94719
The variable $file['name']
is the name of the file as posted in the upload (e.g. the filename from the local system).
The variable $file['tmp_name']
is the name of the file on the server. This is the name of the file that contains the content you want to put into the database.
That should address the php side. The Objective-C code is a little more complicated. You need to manually construct the mime-encoded content that is being used in the upload. There is a simple example on CocoaDev in the generateFormData method, in the line example:
[post_dict setObject:[NSURL fileURLWithPath:@"/Butterfly.tif"] forKey:@"file1"];
he is setting the PHP equivalent of the $_FILE['file1']
variable here, in your case, you probably want to choose forKey:@"file"
instead.
Edit Self-contained synchronous variant. I have the following parameters: DestUrl
- an NSURL containing the destination, fileName
- an NSString containing the name of the file and finally FileData
, an NSData containing the content of the file.
NSMutableData *data = [[NSMutableData alloc] initWithCapacity:100];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:DestUrl];
[request setHTTPMethod:@"POST"];
// Define the boundary
NSString *boundary = [NSString stringWithFormat:@"weasel_grapple_%ld_foo", (long)time(NULL)];
// Tell MIME that content type
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]
forHTTPHeaderField:@"Content-Type"];
// First piece of data
[data appendData:[[NSString stringWithFormat:@"--%@\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];
// Add in the form field name (name), filename
[data appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n",
@"file", fileName] dataUsingEncoding:NSASCIIStringEncoding]];
// And it's binary data here
[data appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\n\n"] dataUsingEncoding:NSASCIIStringEncoding]];
// This is content of the file
[data appendData:FileData];
// Need a blank link as a separator of form-data items
[data appendData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSASCIIStringEncoding]];
// Mark the end of the upload message
[data appendData:[[NSString stringWithFormat:@"--%@--\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *error;
NSData *retData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
If you want to take a file URL, and turn it into FileData then do: NSData *FileData = [NSData dataWithContentsOfFile:fileURL];
On the php side, test first with a simple upload form, making sure that uploaded data is getting into the database and work from there.
My snippet of file upload to the database on the php side looks like. It's pretty horrific, but it gets the upload across the line.:
foreach ($_FILES as $key => $value) {
$file_tag = $key;
$fname = basename($value['name']);
$name = $value['tmp_name'];
$testpage = file_get_contents($name);
$testpage = addslashes($testpage);
mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')");
}
Note also: If your upload file is larger than 64K then you must specify Document
as a field type of MEDIUMBLOB
. The regular BLOB
data type supports up to 64K
data only.
Upvotes: 1