2011-04-08 112 views
1

我有一个图片上传应用程序,我想确保用户上传的图像没有部分(损坏)图像,如果它崩溃或它们在上传时关闭。服务器和应用程序的文件大小不匹配?

我的第一个想法是检查服务器上的文件大小,以查看文件大小是否匹配。这是我的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/) 
+0

'349632'完全可以被8整除。我打算保证从服务器获得的文件大小是“磁盘大小”而不是“文件大小”。你可以发布几个不同的文件返回的大小? – drudge 2011-04-08 22:51:41

+0

添加了一些结果... – iosfreak 2011-04-08 23:07:52

+0

@ phpnerd211:奇怪的文件大小。 – drudge 2011-04-08 23:12:49

回答

1

编辑:新的答案

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

你做上传的文件,因为它被保存:

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

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

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

考虑编码

好像你的编码旧的答案是错的 - 这通常会稍微不匹配的数据。

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

+0

试图将其更改为二进制文件,但是MD5哈希和文件大小仍然不匹配。 – iosfreak 2011-04-09 19:11:08

+0

啊......但上传的图片是否正常工作? – Eiko 2011-04-09 19:15:31

+0

他们正在正确上传。 – iosfreak 2011-04-09 19:17:02

2

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

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

+0

刚发布了一些。如果它们是不同的数字,MD5不会有什么不同吗? – iosfreak 2011-04-08 22:45:55

+2

@ phpnerd211:您可以MD5文件的内容,而不是文件大小。 – drudge 2011-04-08 22:52:44

+0

我熟悉在PHP中使用带有文本字符串的MD5,如:md5(“stuff here”)。你将如何使用objective-c中的JPEG文件做到这一点?谢谢! – iosfreak 2011-04-09 01:21:24

相关问题