开发者

File sizes from server and app aren't matching?

I have a picture uploading app that I want to make sure a user uploads the image without a partial (corrupt) image if it crashes or they close while it's uploading.

My First thought was to check the file sizes on the server to see if the file size match. Here's my OBJECTIVE-C code:

NSString *jpgPath = [NSString stringWithFormat:@"Documents/%@",sqlImageUploadPathTwo];
NSString *yourPath = [NSHomeDirectory() stringByAppendingPathComponent:jpgPath];
NSFileManager *man = [[NSFileManager alloc] init];
NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
int result = [attrs fileSize];
NSLog(@"Initial size: %d", result);

And here's my PHP code:

$file_size = $_FILES['userfile']['size'];

After logging them, I see they appear to be different numbers (but they are somewhat close). I got 337534 from the iPhone app and 349632 from the server.

Why aren't these numbers matching? What would be my best other alternative be?

Thanks!

Coulton

EDIT:

Here's some code for my upload:

UIImage *tempImage = [UIImage imageNamed:jpgPathTwo];
NSLog(@"tempImage: %@", tempImage);
NSData *imageData = UIImageJPEGRepresentation(tempImage, 90);

NSString *urlString = [NSString stringWithFormat:@"http://myflashpics.com/iphone_processes/upload.php?album=%@&id=%@&caption=%@&orient=%@",getAlbumID,theUserID,theCaption,getOrientation];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
N开发者_运维百科SString *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=\".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];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

And here's some PHP:

// Start Uploading...
$uploadDir = "./../users/$get_username/pictures/";
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$newName = $uploadDir . $new_id . $uploadFile;
$file_size = $_FILES['userfile']['size'];

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
// Some code to process and make MYSQL queries
}

EDIT 2:

Here's a table of some successful image uploads:

Size on app - Size on server
337534      - 349632
390995      - 396789
171797      - 176577
171042      - 179143

Sorry, it was uploading the same image. Just updated the results.

Edit 3:

PHP code to do MD5 HASH:

// I've tried this:
md5("$tmp_location");
// And this:
md5_file("$tmp_location");

iPhone app code:

CFStringRef md5hash = FileMD5HashCreateWithPath((CFStringRef)yourPath, FileHashDefaultChunkSizeForReadingData);
NSLog(@"MD5 hash of file at path \"%@\": %@", yourPath, (NSString *)md5hash);
CFRelease(md5hash);

(using this: http://www.joel.lopes-da-silva.com/2010/09/07/compute-md5-or-sha-hash-of-large-file-efficiently-on-ios-and-mac-os-x/)


An alternative is MD5 hash. It is probably the best alternative.

How do you upload the images? Could you provide a code snippet?


Edit: new answer

I suppose the file sizes are different because simply upload different data.

You do not upload the file as it is saved:

UIImage *tempImage = [UIImage imageNamed:jpgPathTwo];
NSLog(@"tempImage: %@", tempImage);
NSData *imageData = UIImageJPEGRepresentation(tempImage, 90);

imageData is not guaranteed to be the same as on disk.

Why not using the data from disk directly? Use [NSData dataWithContentsOfFile:imagePath]; instead.

Old answer considering encoding

It seems like your encoding is wrong - this typically gives slightly mismatching data.

I haven't tried it, but would suggest using binary instead of application/octet-stream. If this doesn't fix the problem, maybe the server side part of saving gets it wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜