2015-02-10 72 views
1

我有一個腳本,目前上傳從互聯網或屏幕截圖保存的圖片。但是,任何使用iPhone相機拍攝的照片都無法上傳。爲什麼會這樣?這是一個記憶問題嗎?圖片無法上傳相機圖片但上傳截圖和保存的圖片

<?php 
$uploaddir = './upload/'; 
$file = basename($_FILES['userfile']['name']); 
$uploadfile = $uploaddir . $file; 
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
{ 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
{ 
    echo "yes"; 
} 
} 
else 
{ 
echo $_FILES['userfile']['error']; //just returns "1" 

} 
?> 

這裏是iOS的:

NSUserDefaults *defaujez = [NSUserDefaults standardUserDefaults]; 
    NSString *rambli = [defaujez objectForKey:@"getpicforfu"];  


    NSData *imageData =UIImageJPEGRepresentation(imageView.image, 90); 
    NSString *imgPath= @"http://www.mywebsite.com/script.php"; 

     NSString *filenames = rambli; 
     NSLog(@"%@", filenames); 
     NSString *urlString = imgPath; 

     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
     [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 

     NSString *boundary = @"---------------------------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 stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]]; 

     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", rambli] dataUsingEncoding:NSUTF8StringEncoding]]; 

     // [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

     [body appendData:[@"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]]; 
     // setting the body of the post to the reqeust 
     [request setHTTPBody:body]; 
     // now lets make the connection to the web 
     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
     if([returnString isEqualToString:@"yes"]) 
     { 
      [self finitop]; 

     } 
     else { 
      [spinner stopAnimating]; 
      rambolrqa.text = @"Posted Empty Because Upload Failed"; 

     } 
+0

您從不打擾檢查上傳是否成功。在'$ _FILES'中有'['error']'參數,原因如下:http://php.net/manual/en/features.file-upload.errors.php – 2015-02-10 14:27:53

+0

上傳失敗。我去了服務器的位置,並檢查。它不在那裏。 – user1667601 2015-02-10 14:44:57

+0

所以檢查'['error']'來找出爲什麼失敗。 – 2015-02-10 14:46:24

回答

0

這通過壓縮iOS上到一個合理的規模來解決它。

NSData *imageDataf = [[NSData alloc] initWithData:UIImageJPEGRepresentation((imageView.image), 0.5)]; 
int imageSize = imageDataf.length; 

if (imageSize >50000) { 
    //35414 normal 
    //952683 camera 
    if (imageSize >2000000) { 
NSData *imgData= UIImageJPEGRepresentation(imageView.image,0.00001); 
UIImage *image=[UIImage imageWithData:imgData]; 
imageView.image = image; 
    } 
    else { 
     NSData *imgData= UIImageJPEGRepresentation(imageView.image,0.001); 
     UIImage *image=[UIImage imageWithData:imgData]; 
     imageView.image = image; 
    } 
}