简体   繁体   中英

Cannot upload file: move_uploaded_file(): Unable to move '/tmp/phpYpUvDT' to 'var/www/libreria

I already checked widely on SO and on the web but I was unable to sort this out and i'm feeling stuck.

This is the PHP code (it's pretty raw) i'm using to show an upload button for book files:

    $partpath = "var/www/libreria/lib-folder/";

 if (isset($_POST['upload'])){
    $file = $_FILES['file'];
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];


    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('epub', 'mobi', 'pdf');

    if (in_array($fileActualExt, $allowed)){
        if ($fileError === 0) {
            if ($fileSize < 100000000) {
                $fileDestination = $partpath.$fileName;
                move_uploaded_file($fileTmpName, $fileDestination);
                echo "Upload completato <br><br>";
            } else {
                echo "File too big <br>";
            }
        } else {
            echo "Error with file: error code ".$fileError." <br>";
        }
    } else {
        echo "You cannot upload this <br>";
    }

}

And I keep getting error:

Warning: move_uploaded_file(var/www/libreria/lib-folder/Mongoose for Application Development.pdf): failed to open stream: No such file or directory in /var/www/libreria/lib-folder/index.php on line 42

Warning: move_uploaded_file(): Unable to move '/tmp/phpg36Ubt' to 'var/www/libreria/lib-folder/Mongoose for Application Development.pdf' in /var/www/libreria/lib-folder/index.php on line 42

I'm running an apache server on an Ubuntu localhost, and I already tried to do the following:

  • setting permission of root foolder www/var to 777 and 755;
  • setting ownership of folder to myself and to www-data;

I don't know what to check or do anymore to be honest, and I would be thankfull if somebody could give me an hint on what i'm doing wrong.

Thanks to anybody that will spend a minute to read and answer:)

It seems uploaded file has a filename that contains space characters. Try replacing all spaces in $filename with "_" before calling move_uploaded_file.

just a guess, but it seems your origin is not existing: Warning: move_uploaded_file(var/www/libreria/lib-folder/Mongoose for Application Development.pdf): failed to open stream: No such file or directory. It seems, there is / missing, right before var:)

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