Laureant
Laureant

Reputation: 1019

PHP upload and download not working for larger files (>1 MB)

I have an upload.php script, that I access from a mobile device, to upload documents to.

Whenever I upload an image of some sort, that is < 1MB, it works perfectly, however, when uploading a file larger than that, it will be corrupted.

The upload script also renames the file, and removes the extension from it, if this could have anything to do with the error...

Here's the script:

<?php header('Access-Control-Allow-Origin: *');

    //Read parameters - I use this when I'm adding the file to the database.
    $documentuniqueid = addslashes($_REQUEST['documentuniqueid']);
    $type = addslashes($_REQUEST['type']);
    $notes = addslashes($_REQUEST['notes']);

    //Get file name
    $filename = urldecode($_FILES["file"]["name"]);


    // Check for errors
    if($_FILES['file']['error'] > 0){
        outputJSON('An error ocurred when uploading.');
    }


    // Check filetype
    //if($_FILES['file']['type'] != 'image/png'){
    //    outputJSON('Unsupported filetype uploaded.');
    //}

    // Check filesize
    if($_FILES['file']['size'] > 500000){
        outputJSON('File uploaded exceeds maximum upload size.');
    }

    // Check if the file exists
    if(file_exists('upload/' . $_FILES['file']['name'])){
        outputJSON('File with that name already exists.');
    }

    // Upload file
    if(!move_uploaded_file($_FILES['file']['tmp_name'], 'documents/'.$documentuniqueid)){
        outputJSON('Error uploading file - check destination is writeable.');
    }

?>

Download script looks like this:

<?php
    sleep(2);
    $document_id = addslashes($_REQUEST['document_id']);

    if($document_id != "") {
        $real_document_name = GetRealFileName($document_id);
        if($real_document_name != "ERROR") {
            $original_filename = "http://www.whatever.com/documents/".$document_id;
            $new_filename = $real_document_name;

            //Headers
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            header('Content-Type: '.finfo_file($finfo, $original_filename));
            header("Content-Length: " . filesize($original_filename));
            header('Content-disposition: attachment; filename="'.$new_filename.'"');      

            //clean up
            ob_clean();
            flush();

            readfile($original_filename);          
            exit();
        }
    }
?>

Any tips on improving this? Or any insight, on why is this not working correctly? You can see that I am renaming the files upon upload, to a random string, and them when downloading, I look up the filenames and rename it back to the original one.

That works as expected for the small file sizes.

I also have to note, that even if I go in manually into the FTP, download the uploaded file and add the right extension myself, I'm unable to open them. The images look messed up, and PDFs, for instance are corrupted.

PHP DETAILS:

both post_max_size and upload_max_filesize is set to 100M. max_file_uploads is set to 20 and file_uploads is ON max_input_time : 60 max_execution_time : 3000

Upvotes: 1

Views: 1753

Answers (3)

Harshad Chhaiya
Harshad Chhaiya

Reputation: 135

Increase the size of upload file property of the php.ini file inside /etc/php5/apache2/.

Set:

upload_max_filesize = 50M

Upvotes: 3

Paulo Pereira
Paulo Pereira

Reputation: 211

To keep the original name of file you need to change

move_uploaded_file($_FILES['file']['tmp_name'], 'documents/'.$documentuniqueid)

to move_uploaded_file - move_uploaded_file($_FILES['file']['tmp_name'], $_FILES['file']['name'])

or write this line in upload script to see the information about the file

$_FILES - print_r($_FILES);

sorry for my english, i hope i helped.

Upvotes: 0

Mohammad Alabed
Mohammad Alabed

Reputation: 809

I think you do not have enough space in your host, Try to upload by FTP "FileZilla for example" and check the log messages

also do not forget to check these values

  • upload_max_filesize
  • post_max_size
  • max_input_time
  • max_execution_time

Upvotes: 0

Related Questions