2014-02-08 100 views
0

我在php中有一個用於將文件上傳到服務器的腳本。這是第一次工作,但我不知道爲什麼它不再工作。它沒有顯示錯誤,但文件仍然沒有上傳到我分配用於保存所有上傳文件的目錄中。下面是需要上傳的護理的一部分:無法使用PHP腳本上傳

<?php 
if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Error: " . $_FILES["file"]["error"] . "<br />"; 
    } 
else 
    { 

    echo "Uploaded: " . basename($_FILES["file"]["name"]) . "<br />"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
    //by default the size of the file is in bytes so u need to divide by 1024 in order to bring it to KB 
    echo "Temporarily stored in: " . $_FILES["file"]["tmp_name"] . "<br />"; 
    $target="/file/" . basename($_FILES["file"]["name"]) . "<br />"; 


    } 

    if(move_uploaded_file($_FILES["file"]["tmp_name"], $target)) 
    { 
    echo "<h2>" . "The file has been stored on the server" . "<br />" . "<h2 />"; 
    echo "New storage location is : " . '<a href="/public/files/" >' . $target . '</a>'; 
    ?> 
    <html> 
<body> 
<div align="right"><a href="/public/files/" >Uploaded files</a></div> 
<br /> 
</body> 
</html> 
<?php 
} 
    else 
    { 
    echo "<h2>" . "Error while saving the file to the server." . "<br />" . "File wont be found in the uploaded files directory" . "<br />" . "<h2 />"; 
    echo "The error says: " . $_FILE["file"]["error"] . " What do we do now?" ; 
    echo"<pre>".print_r($_FILES,true)."</pre>"; 
    ?> 

<? 
+0

所以它會迴應其他任何內容 {以及窗體如何看起來像? –

回答

0

/file/是在服務器的文件系統,幾乎可以肯定不存在的根目錄。 move_uploaded_file()在文件系統級別工作,絕對不知道您的網站的URI結構。你可能想要更多的東西一樣:

move_uploaded_file(...,. $_SERVER['DOCUMENT_ROOT'] . '/file/'); 
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^---- add this 

使得該文件被移動到一個/file子目錄您的網站的根目錄中。