2015-07-13 204 views
0

好吧,非常簡單的上傳腳本從w3schools,我試圖實現,但我不斷收到錯誤消息。move_uploaded_file()拋出錯誤

對於我使用WordPress和創建自定義上傳方法,所以我不能使用自定義字段等需要參考..

HTML節中有關此:

<form method="post" id="form" enctype="multipart/form-data"> 
       <input type="file" name="frontImage" id="frontImage"> 

的事情PHP端:

$target_dir = home_url() . "/wp-content/uploads/survey/"; 
     $target_file = $target_dir . basename($_FILES["frontImage"]["name"]); 
     $uploadOk = 1; 
     $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 

      $check = getimagesize($_FILES["frontImage"]["tmp_name"]); 
      if($check !== false) { 
       $uploadOk = 1; 
      } else { 
       echo "File is not an image."; 
       $uploadOk = 0; 
      } 

// Check if file already exists 
     if (file_exists($target_file)) { 
      echo "Sorry, file already exists."; 
      $uploadOk = 0; 
     } 
// Check file size 
     if ($_FILES["frontImage"]["size"] > 500000) { 
      echo "Sorry, your file is too large."; 
      $uploadOk = 0; 
     } 
// Allow certain file formats 
     if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
      && $imageFileType != "gif") { 
      echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
      $uploadOk = 0; 
     } 
// Check if $uploadOk is set to 0 by an error 
     if ($uploadOk == 0) { 
      echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
     } else { 
      if (move_uploaded_file($_FILES["frontImage"]["tmp_name"], $target_file)) { 
       echo "The file ". basename($_FILES["frontImage"]["name"]). " has been uploaded."; 
      } else { 
       echo "Sorry, there was an error uploading your file."; 
      } 
     } 

好的小號Ø我得到的錯誤是:

"Sorry, there was an error uploading your file." 

這意味着,move_uploaded_file()以部分失敗。

如果我的print_r($ _ FILES)我得到:

Array 
(
    [frontImage] => Array 
     (
      [name] => Everest_kalapatthar_crop.jpg 
      [type] => image/jpeg 
      [tmp_name] => /Applications/MAMP/tmp/php/phpHw9BcP 
      [error] => 0 
      [size] => 93053 
     ) 

) 

是否有任何理由,這是不工作?

+1

什麼是* $ TARGET_DIR *?它是你的目錄的正確路徑嗎?如果是,請確保文件夾權限設置爲* 0777 * –

+0

輸出'$ target_dir'確保它是有效的。 –

+0

@ EM-Creations target_dir和target_file都是正確的路徑 –

回答

0

測試您的目標目錄實際上是否存在使用此代碼寫入。

如果您的目標目錄不可寫,在Mac上,您可以在終端中運行此目錄。

ls -lah /full/path/to/the/survey/directory

,檢查組和所有者,應該由_www喜歡的目錄在你的網站,其餘所有。如果您的Apache用戶不同,請根據需要進行調整。

如果不是的話,那麼運行以下命令:
sudo chown _www:_www /full/path/to/the/survey/directory/
sudo chmod 0755 /full/path/to/the/survey/directory/

<?php 

$target_dir = home_url() . "/wp-content/uploads/survey/"; 
$target_file = $target_dir . basename($_FILES["frontImage"]["name"]); 

// check if dir is writable  
if (!is_writable($target_dir)) { 
    echo $target_dir . ' is not writable'; 
} 
else { 
    // proceed to write the file there 
    // either set a boolean flag $writable = 1 or 
    // insert into if/else chain 
} 

// check if dir exists 
if (!file_exists($target_dir)){ 
    echo $target_dir . ' does not exist'; 
} 
else { 
    // proceed with the rest of your logic 
    // either set a boolean flag $dirExists = 1 or 
    // insert this into your if/else chain 
} 
+0

它說不可寫 –

+0

但是當我去到文件夾說它讀寫我 –

+0

它需要由php/apache用戶寫入,給我一秒我會更新答案 –