uploaded file is too big php

358 views Asked by At

Hi i have the following code that uploads videos to a server and updates the database accordingly. This code works fine when i run it with a bunch of images and or small video's. See the code below:

for ($i=0; $i<count($_FILES['images']['error']); $i++) {
   if ($_FILES['images']['error'][$i] == UPLOAD_ERR_OK) {
       $tmpName = $_FILES['images']['tmp_name'][$i];
       $name = $_FILES['images']['name'][$i];
       $type = $_FILES['images']['type'][$i];
       if (strpos($type, 'image') !== false) {
           $type = "img";
       }elseif(strpos($type, 'video') !== false){
           $type = "vid";
       }else{
           exit();
       }
       move_uploaded_file(($tmpName), $dir.$name);
       $upload = array(
           'name'=>$name,
           'type'=>$type
       );
       $uploads[] = $upload;
   }
}

But when my client tries to upload a video bigger than 64mb the program doesnt upload it... I already tried to change the max_file_size and other according parameters to allow bigger files. But my clients hosting provider doesnt allow this.

So are there any other ways of uploading big files to my server via my custom cms?

Thomas

1

There are 1 answers

0
Edwin On BEST ANSWER

So as said in comments. Reference material is below code examples. Trick is to cut the file into chunks that are less than the upload limit. This method can be extended to the point that when a file upload is interrupted you can continu on the last known part. :-)

Basic JavaScript class to assist in uploading the file, determines the chunks to be sent to a PHP server.

function fileUploader() {
    // Called when the file is selected
    this.onFileSelected = function() {
        // Read file input (input type="file")
        this.file = this.fileInput.files[0];
        this.file_size = this.file.size;
        this.chunk_size = (1024 * 1000);

        this.range_start = 0;
        this.range_end = this.chunk_size;

        this.slice_method = 'slice';
        this.request = new XMLHttpRequest();

        // Start uploading
        this.upload();
    };

    this.upload = function()
    {
        var self = this,
            chunk;

        // Last part reached
        if (this.range_end > this.file_size) {
            this.range_end = this.file_size;
        }

        // Chunk the file using the slice method
        chunk = this.file[this.slice_method](this.range_start, this.range_end);

        // Open a XMLHttpRequest
        var endpoint = "/url/to/php/server/for/processing";
        this.request.open('PUT', (endpoint));
        this.request.overrideMimeType('application/octet-stream');
        this.request.send(chunk);

        // Make sure we do it synchronously to prevent data corruption
        this.request.onreadystatechange = function()
        {
            if (self.request.readyState == XMLHttpRequest.DONE && self.request.status == 200) {
                self.onChunkComplete();
            }
        }
    };

    this.onChunkComplete = function()
    {
        if (this.range_end === this.file_size)
        {
            // We are done, stop uploading
            return;
        }

        this.range_start = this.range_end;
        this.range_end = this.range_start + this.chunk_size;
        this.upload();
    };
}

And for the PHP bit:

...  
$out = fopen("{$filePath}.partial", "a+");                      
fwrite($out, file_get_contents("php://input"));
fclose($out);
...

Big warning here, make sure to properly validate and take security measures to ensure the safety of your clients upload function. You are writing the raw PHP input to a file.

When the upload is done you can rename the file to it's original name including the correct extension.

Reference material:
http://creativejs.com/tutorials/advanced-uploading-techniques-part-1/index.html
https://secure.php.net/manual/en/wrappers.php.php

In a nutshell.. it's break the file into small chunks using a processor, upload the files using conventional methods (like you would normally upload a file), append the input to a temporarily file. Some pitfalls I encountered were sending extra params and alike to the endpoint, avoid those as it's appended to the file and it will corrupt your file.