PHP upload and download not working for larger files (>1 MB)
<p>I have an upload.php script, that I access from a mobile device, to upload documents to.</p>

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

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

<p>Here's the script: </p>

<pre><code>&lt;?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'] &gt; 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'] &gt; 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.');
    }

?&gt;
</code></pre>

<p>Download script looks like this: </p>

<pre><code>&lt;?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();
        }
    }
?&gt;
</code></pre>

<p>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. </p>

<p>That works as expected for the small file sizes.</p>

<p>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.</p>

<p>PHP DETAILS:</p>

<p>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</p>