Mason
Mason

Reputation: 139

move_uploaded _file not functioning correctly

I made a portion for a website that allows csv files to be uploaded and automatically stored in the mysql database. I set up the move_uploaded_file function so that I could make sure that its uploaded correctly, but every time I try uploading something it fails.

The form

<form enctype="multipart/form-data" action="LoadData.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload File: <input name="uploadedfile" type="file"><br />
File/Table Name: <input name="filename" type="text"><br />
<input name="submit" type="submit" value="Upload File">
</form>

The uploading code

$FileName = $_POST['filename'];
$target_path = "/director/to/all/uploaded/files/" . $FileName;

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
    echo "File uploaded correctly </br>";

    //uploads file to database
    mysql_query("LOAD DATA LOCAL INFILE....."); //long piece of code that uploads the csv file to the database.table
    echo "Table imported <br/>"';
}
else
{
    echo "The file was no uploaded correctly, the table was not imported.";
}

I've checked and the the $_FILES array isn't empty, but the files are not created in the new directory which I've ensured exists and has full permissions. I'm pretty sure my syntax is correct as well; move_uploaded_file(/temporary/file/location/tmp_name.csv, /new/file/location/test.csv) Anyone see where I'm going wrong?

Update 1

The results of print_r($_FILES)

( [uploadedfile] => Array ( [name] => Final1.csv [type] => text/comma-separated-values [tmp_name] => /tmp/phpVnOIUK [error] => 0 [size] => 11607 ) )

Update 2

No solution has been found, if you think you have an easy answer please post it, but for now I`m going to simply try going around the problem.

Upvotes: 0

Views: 332

Answers (1)

Marc B
Marc B

Reputation: 360882

Your code is blindly assuming success. Never assume that an upload succeeded. Your boilerplate upload-handling code should look more like

if ($_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK) {
   // it worked, handle the upload
   ...
} else {
   die("Upload failed with error code: " . $_FILES['uploadedfile']['error']);
}

The error codes are defined here: http://php.net/manual/en/features.file-upload.errors.php

Upvotes: 2

Related Questions