-3

I m trying to upload large files using chunk upload. Gone through a few stack queries and found this code, However, it works perfectly fine for files below 200mb but when I try to upload say 2GB file, it returns This site can’t be reached - Connection was reset error page.

I know I missed the JS part which I couldn't able to figure it out.

HTML goes as :

<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Upload Full Video</label>
      <span class="pure-form-message"> * required</span>
      <input type="file" name="video" class="file_multi_video"   accept="video/*" id="stacked-email" style="width:60%;" placeholder="" />
</form>

Upload.php

set_time_limit(0);
$file2=($_FILES['video']['name']);

$tmpfile = $_FILES['file']['tmp_name'];
$orig_file_size = filesize($tmpfile);
$target_file = 'images/videos/'. $_FILES['video']['name'];
    $chunk_size = 100; // chunk in bytes
    $upload_start = 0;
    $handle = fopen($tmpfile, "rb");
    $fp = fopen($target_file, 'w');
    while($upload_start < $orig_file_size) {
    $contents = fread($handle, $chunk_size);
    fwrite($fp, $contents);

    $upload_start += strlen($contents);
    fseek($handle, $upload_start);
}
    fclose($handle);
    fclose($fp);
    unlink($_FILES['file']['tmp_name']);

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

php_value memory_limit 500M
php_value post_max_size 1000M
php_value upload_max_filesize 500M

Please help me out with the JS code for the above php chunk upload.

Any help is greatly appreciated.

Andy Winarko
  • 645
  • 1
  • 9
  • 20
Harish K
  • 129
  • 2
  • 13
  • Where is the part of this that actually performs a _chunked_ upload? You have just shown a normal form, so without any additional client-side scripting/AJAX, this will do you _normal_ upload. – 04FS Sep 29 '20 at 08:07
  • @04FS can you please help me providing that code. – Harish K Sep 29 '20 at 08:13
  • Some reading : https://stackoverflow.com/questions/33537769/how-to-upload-a-file-with-ajax-in-small-chunks-and-check-for-fails-re-upload-th – Alexandre Elshobokshy Oct 01 '20 at 12:23
  • Take a look here: https://stackoverflow.com/questions/7853467/uploading-a-file-in-chunks-using-html5?rq=1 – Anton Oct 05 '20 at 17:04
  • Please try a different browser if you are using Google Chrome. – Asiri Hewage Oct 07 '20 at 04:42
  • consider using s3 like solution to achieve this. You could have an s3 bucket and accomplish the upload in background using s3 presigned url. If you don't want to bind yourself to aws or similar storage service you could set an minio service along with your php server and simply communicate with urls. Why? chunk upload would mean you'll have to reassemble the file back on the server and also keep track of lost chunks if there are some, so won't be a piece of cake and straight forward solution comparing to bucket solution, where you have things covered out of the box – ihoryam Oct 07 '20 at 10:41

1 Answers1

1

The limit on file size uploads is limited by a LOT more than just PHP. PHP has its limits, Apache has its limits, many web services have their own limits, and then you have the limit on the /tmp directory size which could be set by user permissions on a shared host. Not to mention just running out of hard drive space!

In your case,

    #CHANGE THESE VALUES
    php_value memory_limit 500M
    php_value post_max_size 1000M
    php_value upload_max_filesize 500M
    
    #TO SOMETHING LIKE THIS DEPENDS ON YOUR REQUIREMENTS
    php_value memory_limit 2GB
    php_value post_max_size 2GB
    php_value upload_max_filesize 2GB

Also you can set these values in your php.ini ( my location is /etc/php/7.0/apache2/php.ini ) sometime service provider does not allow to override php.ini setting with the .htaccess settings

As you also mention chunk upload in your question, check this link you will find good answers here.

Aabir Hussain
  • 1,161
  • 1
  • 11
  • 29