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条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法