2011-12-14 56 views
0

我正在爲iOS應用程序編寫圖片上傳的PHP/webserver端。使用html文件,我可以將圖片上傳到我的腳本中。我甚至寫了一個Perl LWP腳本來發布沒有問題的圖像。PHP is_uploaded_file iOS應用程序失敗

當我調用is_uploaded_file時,iOS應用上傳圖片失敗。在json響應中發回$ _FILES var向我們展示了我們對文件上傳的期望。我也在tmp_name上做了一個file_exists,並且也失敗了。

在請求中還有什麼可以查看來確定哪裏出了問題?我希望能夠指出發佈請求有什麼問題。

在此處,它停止處理圖片上傳的代碼塊:

//Check for valid upload 
    if(!is_uploaded_file($_FILES['photo']['tmp_name'])) { 
     $this->errors['upload_2'] = $photoErrorString; 
     $this->errors['files'] = $_FILES; 
     $this->errors['image_uploaded'] = file_exists($_FILES['photo']['tmp_name']); 
     error_log("uploadPhoto: upload_2"); 
     return false; 
    } 
+0

什麼樣的價值是`$ _FILES [ '照片'] [ '錯誤']`?如果它不是「0」,則上傳失敗。錯誤代碼記錄在這裏:http://php.net/manual/en/features.file-upload.errors.php。檢查臨時文件的存在是驗證上傳是否成功的一個壞方法。 – 2011-12-14 05:14:14

+0

@MarcB在我上面發佈的檢查之前,我確實有`if($ _ FILES ['photo'] ['error']!= UPLOAD_ERR_OK){`在is_uploaded_file檢查之前,它一直表示上傳是正常的。 – devNoise 2011-12-14 14:53:29

回答

1

據我知道你不能上傳iOS應用中的圖像或照片直接使用PHP腳本。

您必須從iOS應用程序將64位編碼發送到負責文件上載的腳本,作爲簡單的POST。然後,該腳本將首先解碼您的照片的字符串,然後PHP腳本將從字符串中創建圖像以僅上傳。

代碼將是這樣的:

$filedb = $path_to_dir.$file_name_of_the_photo; 
$profile_pic = $_POST['profile_pic']; 
$profile_pic= base64_decode($profile_pic); 
if(($img = @imagecreatefromstring($profile_pic)) != FALSE) 
{ 
    if(imagepng($img,$filedb))//will create image and save it to the path from the filedb with the name in variable file_name_of_the_photo 
    { 
imagedestroy($img);// distroy the string of the image after successful upload 
// do other updates to database if required 
    } 
    else //cannot create imagepng Error 
    { 
//do what required 
    } 
} 

希望這會工作。

+0

謝謝。這是正確的道路,事實上圖像是原始發送而不是base64編碼。 – devNoise 2011-12-22 15:11:59