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


File sizes from server and app aren't matching?

我有一个图片上传应用程序,我想确保用户上传的图像没有部分(损坏)的图像,如果它崩溃或关闭,而它的上传。

我的第一个想法是检查服务器上的文件大小,看看文件大小是否匹配。下面是我的OBJECTIVE-C代码:

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);
下面是我的PHP代码:
$file_size = $_FILES['userfile']['size'];

在记录它们之后,我看到它们似乎是不同的数字(但它们有些接近)。我从iPhone应用程序得到337534,从服务器得到349632。

为什么这些数字不匹配?我最好的其他选择是什么?

谢谢!
Coulton

编辑:

这是我上传的一些代码:

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:@"'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];

这里是一些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
}

编辑2:

下面是一些成功图片上传的表格:

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

对不起,上传的是同一张图片。刚刚更新了结果

编辑3:

PHP代码做MD5 HASH:

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

iPhone应用程序代码:

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

另一种方法是MD5哈希。这可能是最好的选择。

如何上传图片?你能提供一个代码片段吗?

编辑:新的答案

我想文件大小不同是因为上传的数据不同。

你做而不是上传文件,因为它是保存的:

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

imageData不能保证与磁盘上的相同。

为什么不直接使用磁盘上的数据?用[NSData dataWithContentsOfFile:imagePath];代替。

考虑编码的旧答案

似乎你的编码是错误的-这通常会给出稍微不匹配的数据。

我没有尝试过,但建议使用binary而不是application/octet-stream。如果这不能解决问题,可能是服务器端的保存部分出错了。