execv
execv

Reputation: 849

PHP: move_uploaded_file partially uploading

I am using the following code to upload a CSV to my server and process it. Occasionally (and very randomly), the file that is uploaded only contains half of the number of rows the original file contains (the original file contains 450 rows, but some of my uploads only contain ~200 rows). move_file_upload() is returning true, and this is happening before the CSV is processed. Any thoughts or ideas why? Yes, permissions are all correct.

// PHP:
public function import() {
    if ($this->request->is('post')) {
        $destination = "./uploaded_csvs/reports/" . time()  . "_" . $this->request->data['Reports']['file']['name'];

        // Copy the report to the server
        if(move_uploaded_file($this->request->data['Reports']['file']['tmp_name'], $destination)) {
            // Success - Process CSV
        } else {
            // Error
        }
    }
}

// HTML:
<form accept-charset="utf-8" method="post" enctype="multipart/form-data" id="ReportsForm" action="/Reports/import">
    <input type="file" id="ReportFile" size="7" value="" name="data[Report][file]">
    <input type="image" src="/img/buttons/save.png">
</form>

Upvotes: 0

Views: 124

Answers (1)

Marc B
Marc B

Reputation: 360602

move_upload_file will NOT truncate a file. It'll simply move whatever was uploaded. If the file's coming out short, then that's what the server received.

The very FIRST thing you should ALWAYS do with an upload is verify that it actually succeeded:

if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
   die('Upload failed with error code #' . $_FILES['file']['error']);
}

Never assume success. Always check for failure, and treat success as a pleasant surprise.

Upvotes: 1

Related Questions