2016-10-02 98 views
0

我是PHP新手,仍在學習它......今天我無法弄清楚爲什麼文件要從目標目錄 - $ target_dir上傳。我改變了$ target_dir很多但我總是得到相同的結果..代碼看起來很好..將文件上傳到目標文件夾時出現問題

任何想法?

謝謝。

function avatarUpload(){ 

    $target_dir = "../uploads/avatars/"; 
    $target_file = basename($_FILES["fileToUpload"]["name"]); 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    // Check if image file is a actual image or fake image 
    if(isset($_POST["avatar"])) 
    { 

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

    } 

    // Check if file already exists 
    if (file_exists($target_file)) 
    { 
     do 
     { 
      $rand = rand(100,10000); 
      $target_file = $rand .= $target_file; 
     } 
     while(file_exists($target_file)); 
    } 

    // Check file size 
    if ($_FILES["fileToUpload"]["size"] > 5000000000) 
    { 
     //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; 

    } else { 

     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)== true) 
     { 

      // echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
      return $target_file; 

     } else 
     { 
      // echo "Sorry, there was an error uploading your file."; 

      return $target_file; 

     } 
    } 


    return $target_file; 
    } 
+0

什麼是上傳? – Cyclotron3x3

+0

一切都好,只是該文件夾是錯過..我正在將文件的根.. –

+0

檢查您的路徑,嘗試將其更改爲絕對,然後運行。 '../'似乎造成問題 – Cyclotron3x3

回答

0

你忘了追加目標目錄文件名。

$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]); 
+0

ahhhh ..你有權利....謝謝! –

+0

如果您的問題得到解決,您會介意接受答案。這將有助於解決類似問題的未來讀者 – Cyclotron3x3

相關問題