2014-10-09 60 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!"; 
    } 
?>