Resume or Pause File Uploads in PHP

By: micronix @ gmx . ne  

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"; 
        } 
? >



Archived Comments


Most Viewed Articles (in PHP )

Retrieve multiple rows from mysql and automatically create a table in PHP

Function to force strict boolean values in PHP

Building PHP 5.x with Apache2 on SuSE Professional 9.1/9.2

Warning: session_start(): open .... failed - PHP error

PHP ./configure RESULTING IN [email protected]_2_2_3_... AND UNRESOLVED REFERENCES WITH ORACLE OCI8

PHP 5.1.4 INSTALLATION on Solaris 9 (Sparc)

Installing PHP 5.x with Apache 2.x on HP UX 11i and configuring PHP 5.x with Oracle 9i

Cannot load /usr/local/apache/libexec/libphp4.so into server: ld.so.1:......

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

error: "Service Unavailable" after installing PHP to a Windows XP x64 Pro

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform

Function to return number of digits of an integer in PHP

Function to sort array by elements and count of element in PHP

Function to convert strings to strict booleans in PHP

Convert IP address to integer and back to IP address in PHP

Latest Articles (in PHP)

Comment on this tutorial