2015-12-29 58 views
0

我有一個錯誤。我用HTML 5和PHP做了一個文件上傳。當我嘗試上傳任何內容時總會出現錯誤。下面的代碼:php flie上傳錯誤

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    Select file to upload: 
    <input type="file" name="fileToUpload" id="fileToUpload"> 
    <input type="submit" value="Upload your file" name="submit"> 
</form> 

upload.php的:

$target_path = "upload/"; 
$target_path = $target_path . basename($_FILES["fileToUpload"]["name"]); 

if(isset($_POST["submit"])) { 
    if (file_exists($target_file)) { 
     echo "Sorry, file already exists."; 
     $uploadOk = 0; 
    } 

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_path)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been         uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
+0

的目標部分,你在哪裏最初定義'是在'如果(file_exists($ TARGET_FILE使用$ target_file' )){'?我想你需要在那裏使用'$ target_path'!此外,你應該在你的問題中包含錯誤,而不僅僅是「總是有錯誤」 – RamRaider

+0

請按照我希望它對你有用的鏈接。 http://stackoverflow.com/questions/1673393/php-file-upload –

+0

只需更改move_uploaded_file調用的目標部分的文件名\t 'if(move_uploaded_file($ _ FILES [「fileToUpload」] [「tmp_name」], $ target_file))' – Codeone

回答

0

如在評論中提及,在邏輯測試中使用的變量並沒有任何地方所定義。我相信你打算這樣。

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    Select file to upload: 
    <input type="file" name="fileToUpload" id="fileToUpload"> 
    <input type="submit" value="Upload your file" name="submit"> 
</form> 
<?php 

    $target_path = "upload/"; 
    $target_file = $target_path . basename($_FILES["fileToUpload"]["name"]); 

    if(isset($_POST["submit"])) { 

     if (file_exists($target_file)) { 
      echo "Sorry, file already exists."; 
      $uploadOk = 0; 
     } 
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
      echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
     } else { 
      echo "Sorry, there was an error uploading your file."; 
     } 
    } 
?> 
0

只要改變move_uploaded_file呼叫

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
+0

仍然無法正常工作。也許不是關於我在RPI上的服務器,但我不這麼認爲。 –