文件上载脚本正常运行,但文件不在目录中

时间:2014-09-24 13:23:51

标签: php

我正在使用实际正常运行的文件上传脚本,但是当我检查我正在上传文件的目录时,它不会出现。我的脚本出了什么问题?

upload_form.php

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

upload.php的

$target = "/uploads/";
$path = $target.basename($_FILES["image"]["name"]);
if($_POST["upload"]){
    if(move_uploaded_file($_FILES["image"]["tmp_name"], $target)){
        echo "The file has been uploaded.";
    }
    else{
        echo "There was an error.";
    }
}

uploads文件夹与upload.php文件位于同一目录中。正如我所提到的,脚本运行完全正确,因为我看到与if语句关联的消息。那么,我做错了什么?如果我将目录更改为uploads/,我会收到以下错误:

Warning: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: The second argument to copy() function cannot be a directory in C:\wamp\www\TouchKiosk-ver2\php\upload.php on line 5

Warning: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move 'C:\wamp\tmp\php5F6F.tmp' to 'uploads/' in C:\wamp\www\TouchKiosk-ver2\php\upload.php on line 5 

1 个答案:

答案 0 :(得分:2)

如评论中所述,如果文件夹与脚本位于同一目录中,则无需以反斜杠开头。

此外,您不使用自己的$path变量,仍然使用没有文件名的$target。在您的代码中,更改move_uploaded_file以改为使用$path

$target = "uploads/";
$path = $target.basename($_FILES["image"]["name"]);
if($_POST["upload"]){
    if(move_uploaded_file($_FILES["image"]["tmp_name"], $path)){
        echo "The file has been uploaded.";
    }
    else{
        echo "There was an error.";
    }
}