Skip to content
Advertisement

move_uploaded_file not working on Apache server in Linux Mint

So I am trying to do a basic upload via an HTML form and a simple php script, but the move_uploaded_file function always returns false. I have run “chmod 777” on the directory (I will deal with safety more when I actually get this to work) and the “upload” directory is in the htdocs folder (actually /var/www in Ubuntu Server and Linux Mint).

Here is the form:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

…and upload_file.php…

<?php
    if ($_FILES["file"]["error"] == 0){
        if (file_exists("upload/" . $_FILES["file"]["name"])){
            echo $_FILES["file"]["name"] . " already exists. ";
        }else{
            if(move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/upload/" . $_FILES["file"]["name"])){ 
                echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
            }else{
                echo "Failed to move uploaded file";
            }
        }
    }else{
        echo "Return Code: " . $_FILES["file"]["error"];
    }
?>

When I try to upload a small JPEG, I get “Failed to move file”. Any thoughts?

Advertisement

Answer

Your script needs to be able to write to the destination directory, which in this case would be /var/www/upload/ (is that the directory where you changed the permissions?). Also you are using the client’s local name which could be a possible problem and security issue (not necessarily the reason here though).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement