我有一个图片上传应用程序,我想确保用户上传的图像没有部分(损坏)图像,如果它崩溃或它们在上传时关闭。服务器和应用程序的文件大小不匹配?
我的第一个想法是检查服务器上的文件大小,以查看文件大小是否匹配。这是我的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个。
为什么这些数字不匹配?我最好的其他选择是什么?
谢谢!
库尔顿
编辑:
下面是我上传的一些代码:
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哈希值:
// 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/)
'349632'完全可以被8整除。我打算保证从服务器获得的文件大小是“磁盘大小”而不是“文件大小”。你可以发布几个不同的文件返回的大小? – drudge 2011-04-08 22:51:41
添加了一些结果... – iosfreak 2011-04-08 23:07:52
@ phpnerd211:奇怪的文件大小。 – drudge 2011-04-08 23:12:49