2014-10-09 61 views
-3

我有一個非常簡單的文件上傳形式和PHP腳本,但它似乎沒有工作。move_uploaded_file()不起作用

HTML:

<form enctype="multipart/form-data" method="post" action="upload_file.php"> 
    Send this file: <input name="userfile" type="file" /><br /> 
    <input type="submit" value="Send File" /> 
</form> 

PHP:

<?php 
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], "./upload")) { 
     print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}"; 
    } else { 
     print "Upload failed!"; 
    } 
?> 

當我上傳的文件,並參觀了'upload_file.php「頁面我得到這個錯誤:

Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in blahhhh/blahhhh/public_html/upload_file.php on line 2 Warning: move_uploaded_file(): Unable to move '/tmp/phprsYav7' to './upload' in blahhhh/blahhhh//public_html/upload_file.php on line 2 Upload failed!

任何想法如何解決這個問題?謝謝!

+0

你*讀*錯誤信息?你需要提供一個*文件路徑*,而不僅僅是一個目錄。像'upload/foobar.jpg',而不僅僅是'upload'。 – deceze 2014-10-09 18:02:08

+0

在這裏,閱讀'F'手冊http://php.net/manual/en/function.move-uploaded-file.php - 你錯過了一個參數。見示例#1。 – 2014-10-09 18:03:43

+0

@deceze哦我明白了,如何獲取它,以便文件名是上傳到./upload目錄時的'tmp名稱'? - 如果可能的話> – 2014-10-09 18:07:32

回答

1

你需要指定一個文件名,而不僅僅是一個路徑。這樣的事情:

<?php 
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], "./upload/blah.txt")) { 
     print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}"; 
    } else { 
     print "Upload failed!"; 
    } 
?>