PHP move_uploaded_file

时间:2013-11-03 12:36:10

标签: php file-upload

我正在尝试使用PHP move_uploaded_file将文件上传到服务器,我收到以下错误:

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /Users/Rick/Sites/upload/upload.php on line 7

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpDlCZUd' to '/Users/Rick/Sites/upload/uploads/richardgregson' in /Users/Rick/Sites/upload/upload.php on line 7

下面是我的代码,没什么复杂的。

if($_POST["upload"]){

$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"];

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
        echo "<div class='success'>The file " . "<span class='filename'>" . basename( $_FILES['uploadedfile']['name']) . "</span>" . " has been uploaded</div>";
    } else {
        echo "<div class='error'>There was an error uploading the file, please try again!</div>";
    }
}

它正在写入的文件夹的权限是正确的。我不明白错误“不能是一个目录,因为参数是我们将文件移动到哪里所以它必须是一个目录..”

由于

瑞克

1 个答案:

答案 0 :(得分:0)

错误告诉您错误的原因 - $target_path(/ Users / Rick / Sites / upload / uploads / richardgregson)是一个目录

尝试添加扩展程序。

$extension=end(explode('.', $_FILES["uploadedfile"]["name"]));
$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '.' . $extension;

或许您将文件放入文件夹

$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '/' . $_FILES["uploadedfile"]["name"];

注意:你不应该相信用户只上传你想要的文件,你应该清理$ _POST [&#34; name&#34;],这太容易了,&#39; ../ .. /&#39;并发布一个PHP文件。