php中的文件上传错误

时间:2011-12-06 16:18:41

标签: php javascript html

如何通过文件上传选项将文件传递到服务器?

i)我使用如下形式:

<form name = "someForm" id = "someForm" method = "post" action = "saveFile.php">                
    <input type = "file" name = "upload1" id = "upload1" />
    <input type = "file" name = "upload2" id = "upload2" />                     
    <input type = "file" name = "upload3" id = "upload3" />
    <input type = "submit" id = "btnSubmit" value = "Submit" />
</form>

ii)在saveFile.php中,我使用:

说:

echo $_FILES['upload1']["size"]; 

除了其他人,但我得到未定义索引:upload1 错误,但不是,当我使用:

echo $_POST['upload1'];  //returns filename

3 个答案:

答案 0 :(得分:9)

您需要将<form> enctype指定为"multipart/form-data"

<form enctype = "multipart/form-data" name = "someForm" id = "someForm" method = "post" action = "saveFile.php">                
    <input type = "file" name = "upload1" id = "upload1" />
    <input type = "file" name = "upload2" id = "upload2" />                     
    <input type = "file" name = "upload3" id = "upload3" />
    <input type = "submit" id = "btnSubmit" value = "Submit" />
</form>

答案 1 :(得分:4)

<form name="someForm" id="someForm" method="post"
    action="saveFile.php" enctype="multipart/form-data">

上传文件需要输入。

您可以$_FILES而不是$_POST访问它们,也不能$_GET访问它们:

echo $_FILES['upload1']['size'];

答案 2 :(得分:2)

文件保存在:

print_r($_FILES); // NOT IN POST