Programming Tutorials

Resume or Pause File Uploads in PHP

By: micronix @ gmx . ne in PHP Tutorials on 2012-12-05  

I have found the Solution for Resume or Pause Uploads. In this Code Snippet it is the Server Side not Client on any Desktop Program you must use byte ranges to calculate the uploaded bytes and missing of total bytes.

Here the PHP Code

<?php 
$CHUNK = 8192; 

        try { 
            if (!($putData = fopen("php://input", "r"))) 
                throw new Exception("Can't get PUT data."); 

            // now the params can be used like any other variable 
            // see below after input has finished 

            $tot_write = 0; 
            $tmpFileName = "/var/dev/tmp/PUT_FILE"; 
            // Create a temp file 
            if (!is_file($tmpFileName)) { 
                fclose(fopen($tmpFileName, "x")); //create the file and close it 
                // Open the file for writing 
                if (!($fp = fopen($tmpFileName, "w"))) 
                    throw new Exception("Can't write to tmp file"); 

                // Read the data a chunk at a time and write to the file 
                while ($data = fread($putData, $CHUNK)) { 
                    $chunk_read = strlen($data); 
                    if (($block_write = fwrite($fp, $data)) != $chunk_read) 
                        throw new Exception("Can't write more to tmp file"); 

                    $tot_write += $block_write; 
                } 

                if (!fclose($fp)) 
                    throw new Exception("Can't close tmp file"); 

                unset($putData); 
            } else { 
                // Open the file for writing 
                if (!($fp = fopen($tmpFileName, "a"))) 
                    throw new Exception("Can't write to tmp file"); 

                // Read the data a chunk at a time and write to the file 
                while ($data = fread($putData, $CHUNK)) { 
                    $chunk_read = strlen($data); 
                    if (($block_write = fwrite($fp, $data)) != $chunk_read) 
                        throw new Exception("Can't write more to tmp file"); 

                    $tot_write += $block_write; 
                } 

                if (!fclose($fp)) 
                    throw new Exception("Can't close tmp file"); 

                unset($putData); 
            } 

            // Check file length and MD5 
            if ($tot_write != $file_size) 
                throw new Exception("Wrong file size"); 

            $md5_arr = explode(' ', exec("md5sum $tmpFileName")); 
            $md5 = $md5sum_arr[0]; 
            if ($md5 != $md5sum) 
                throw new Exception("Wrong md5"); 
        } catch (Exception $e) { 
            echo '', $e->getMessage(), "\n"; 
        } 
? >





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

PHP code to write to a CSV file from MySQL query

Different versions of PHP - History and evolution of PHP

PHP code to import from CSV file to MySQL

Send push notifications using Expo tokens in PHP

A Basic Example using PHP in AWS (Amazon Web Services)

Encrypting files using GnuPG (GPG) via PHP

PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......

Decrypting files using GnuPG (GPG) via PHP

Parent: child process exited with status 3221225477 -- Restarting

Error: Length parameter must be greater than 0

Password must include both numeric and alphabetic characters - Magento

Convert a hex string into a 32-bit IEEE 754 float number in PHP

Convert XML to CSV in PHP

PHP file upload prompts authentication for anonymous users

Reading word by word from a file in PHP

Latest Articles (in PHP)