doujinyi1267 2011-04-08 22:33
浏览 46
已采纳

服务器和应用程序的文件大小不匹配?

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"];
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:@"
--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
", 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/)
  • 写回答

2条回答 默认 最新

  • dongzheng7165 2011-04-09 19:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的