Patrick Bokhove
Patrick Bokhove

Reputation: 31

PHP move_uploaded_file function very picky

I having trouble with the move_uploaded_file function on my website.

The whole idea is that in a form, i insert the title, description and a screenshot of a project. Everything is working just fine. It saves the title and description with an id and the creation date in a mysql database and moves the file to a folder on the server.

The problem however is that some files are transferred to folder and some aren't. The problem seems to be in the files, but i can't seem to figure out what the problem is.

It is not the filesize; i have files of 5/6MB that are placed in the folder without any problems, and i have files that are around 3MB that arent. The extension isn't the problem either, they are both .jpg.

Are there any other requirements that a .jpg file should meet, in order to be uploaded?

I'm 99% certain the problem isn't in my code, as it uploads some files without a problem, but here is my code anyway.

The HTML part:

<form action="" method="post" enctype="multipart/form-data">
    <h2>Title*</h2>
    <input type="text" id="title" name="title">
    <h2>Description*</h2>
    <textarea id="descr" name="descr" cols="40" rows="4"></textarea>
    <h2>Add file*</h2>
    <input type="file" id="file" name="file">
    <h2><input type="submit" id="submit" name="submit" value="Uploaden"></h2>
    <p id="requirements">Fields marked with * are required.</p>
</form>

And this is the PHP part:

if (isset($_POST['submit'])) {

$destination = "../uploadedfiles/" . basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $destination)) {
    echo '<p id="succes">File has been uploaded</p>';
} else {
    echo '<p id="error">File has not been uploaded</p>';
}

$title = $_POST['title'];
$descr = $_POST['descr'];
$name = ($_FILES['file']['name']);

include "../connect/connect.php";

if ($title == "" || $descr == "") {
    echo '<p id="error">Fill in the required fields</p>';
} else {

    $query = "INSERT INTO file(title, descr, name, created) VALUES ('{$title}', '{$descr}', '{$name}', NOW())";

    $upload = $connect->query($query);

    if ($upload) {
        echo '<p id="succes">Info is stored in database</p>';
    } else {
        echo '<p id=error>Failed to store info in database</p>';
    }
}
}

Upvotes: 1

Views: 292

Answers (1)

Danilo Kobold
Danilo Kobold

Reputation: 2581

A bunch of stuff might be going wrong. move_uploaded_file is very pick and sometimes doesn't behave how it is documented. I had problems with it once and honestly, copy archive the same result in most cases if properly implemented.

  • There are reports of lone filenames causing trouble. 249 chars on $destination seems to be the limit.
  • not only upload_max_filesize must be set but also post_max_size
  • utf-8 names might be a problem
  • If you have problems where the uploaded file seems unaccessible, try to use copy() instead. There are reports of people not being able to find the file just after upload, move_uploaded_file just limits path over copy, the results are the same if you dont input any user var.

Upvotes: 0

Related Questions