简体   繁体   中英

PHP move_uploaded_file

I am trying to upload files to a server using PHP move_uploaded_file and I am getting the following error:

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

Below is my code, nothing complicated.

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>";
    }
}

The permissions on the folder it is writing to are correct. I dont understand the error "cannot be a directory as the argument is where we are moving the file to so it has to be a directory.."

Thanks

Rick

The error is telling you what's wrong - $target_path (/Users/Rick/Sites/upload/uploads/richardgregson) is a directory

Try adding an extension.

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

Or perhaps your putting the file into a folder

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

Note: you should not trust the user to upload only the files you want and you should sanitise $_POST["name"], all too easy to make that '../../' and post a PHP file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM