Sean
Sean

Reputation: 6499

Uploading a large file with PHP/JavaScript?

I'm trying to enable uploads of up to around 12MB for a site, unfortunately using a basic form element like:

<input type='file' name='videofile' />

doesn't really seem to cut it, it seems to timeout after a certain period (I've tried upping the server variables, not sure if this is a browser thing).

Are there any readily available HTML5/JavaScript solutions that I can use here? I've tried Googling around but it seems there isn't much information around ... a few people have proposed Flash/Java applets, but that's something I really don't want to get into.

Upvotes: 0

Views: 2091

Answers (5)

sadegh salehi
sadegh salehi

Reputation: 11

I made all the changes in php.ini ,lighttpd but again I could not upload a file of more than 2 megabytes. I wrote a code to transfer the large file step by step and finally merge them together.

1.PHP and Javascript code

    <form  action="upload.php" method="post" enctype='multipart/form-data' >
    <input type="file" id="fileInput" name="file" required >
   <button type="button" onclick="uploadFile()" id="update" style="height:32px" name="update"  class="btn btn-success">Upload File</button>  
</form>
<script>
 function uploadFile() {
     
            var file = document.getElementById('fileInput').files[0];
            var checkSize = 2 * 1024 * 1024;
            var offset = 0;
            var totalSize = file.size;

            function sendcheck() {
                var check = file.slice(offset, offset + checkSize);
                var formData = new FormData();
                formData.append('file', check, file.name);
                formData.append('offset', offset);

                fetch('upload.php', {
                    method: 'POST',
                    body: formData
                }).then(response => response.text()).then(result => {
                    offset += checkSize;
                    if (offset < totalSize) {
                        sendcheck();
                    } else {
                        alert('Upload file done.');
                       
                    }
                }).catch(error => alert('Error:', error));
            }

            sendcheck();
        }

</script>

2-upload.php file is

$uploadDir = 'upload/';           //directory for upload
$checkSize = 2 * 1024 * 1024;       // 2MB 
    if (isset($_FILES['file']) && isset($_POST['offset'])) {
        $file = $_FILES['file'];
        $offset = intval($_POST['offset']);
        $filePath = $uploadDir . "uploadfile.zip";

        if ($file['error'] === UPLOAD_ERR_OK) {
            // Open file in append mode
            $out = fopen($filePath, $offset === 0 ? 'wb' : 'ab');
            if ($out) {
                $in = fopen($file['tmp_name'], 'rb');
                if ($in) {
                    while ($buff = fread($in, 8192)) {
                        fwrite($out, $buff);
                    }
                    fclose($in);
                    fclose($out);
                    echo "File uploaded successfully.";
                } else {
                         echo "Failed to read uploaded File.";
                            
                   
                }
            } else {
                           echo "Failed to open target file.";
                          
            }
        } else {
            
           echo "file Upload error: " . $file['error'];
        }
    } else {
        echo "Faild Upload file";
    }

With the above code, I can upload any file of any size without changing the track.

Upvotes: 0

WolfBright
WolfBright

Reputation: 11

The problem may be with maximum execution time of PHP script (by default 30 seconds). You can change it with function set_time_limit, but it does not work in safe mode.

Upvotes: 0

Twisted1919
Twisted1919

Reputation: 2499

There are 2 directives you should alter in order to increase the allowed upload file size, these are: post_max_size and upload_max_filesize, so one would just post the below snippet of code in the php init script (index.php etc) in order to increase the allowed upload file size.

ini_set('memory_limit','256M'); // this is optional.
ini_set('post_max_size','64M');
ini_set('upload_max_filesize','64M');

L.E: If you are handling real big files, then you should consider chunking: Upload 1GB files using chunking in PHP

Upvotes: 1

lexmihaylov
lexmihaylov

Reputation: 727

I don't think that this is a browser problem. Setting the values in your .htaccess or php.ini should work:

php_value upload_max_filesize 15M
php_value post_max_size 15M

Alternatively you could use plupload

Upvotes: 1

Robert DeBoer
Robert DeBoer

Reputation: 1695

Uploadify makes both an HTML5 and a flash version, you just download the one you want.

Also, be mindful of changing ini settings with PHP as some servers will allow this, others won't, and others will up to a certain size. You will want to check your php settings to see if this is allowed.

Upvotes: 1

Related Questions