Search code examples
phpfilemove-uploaded-file

move_uploaded_file(uploads/): failed to open stream: Is a directory


I have this problem with a script to upload files on the server, the file is called view.php. The server is not localhost, but I have all the permissions thanks to sudo. I already gave all the permissions (777) to the file and also to the directory where he is supposed to upload the files (called uploads/). I know it's not a good practice, but I'm really tying everything! view.php and the folder uploads/ are in the same folder, I don't get what's the problem here, do I have to change the path (original path: /var/www/html/concrete5/application/blocks/insert, both are in the folder called insert)? Hope someone can help me, thanks in advance!

This is my code php:

$target_dir = "uploads/";
    $target_file = $target_dir. basename($_FILE["fileToUpload"]["name"]);
    $uploadOk=1;
    $imageFileType=strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    if($uploadOk == 0){
            echo "Sorry, your file was not uploaded.";
    }else{
    if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){ //--- the problem is here!
            echo "The file ".htmlspecialchars(basename($_FILES["fileToUpload"]["tmp_name"])). " has been uploaded.";
    }else{
    echo "Sorry, there was an error uploading your file";
            }
    }

Solution

  • It looks as though your basename($_FILE["fileToUpload"]["name"]) should be basename($_FILES["fileToUpload"]["name"]) as you've misspelled $_FILES https://www.php.net/manual/en/reserved.variables.files.php which is why it's not returning the correct filename

    :)