sa-tasche
1/27/2020 - 12:55 PM

Download / Upload Large Files with PHP (old)

Download / Upload Large Files with PHP (old)

<?php
/**
* @author     Jack Mason
* @website    volunteer @ http://www.osipage.com, web access application and bookmarking tool.
* @copyright  Free script, use anywhere as you like, no attribution required
*/

/* You may need these ini settings too */
set_time_limit(0);
ini_set('memory_limit', '512M');

//THE DOWNLOAD SCRIPT
$filePath = '/path/to/file';    // set your download file path here.
download($filePath);            // calls download function

function download($filePath)
{
    if(!empty($filePath))
    {
        $fileInfo = pathinfo($filePath);
        $fileName  = $fileInfo['basename'];
        $fileExtnesion   = $fileInfo['extension'];
        $default_contentType = 'application/octet-stream';
        $content_types_list = mimeTypes();
        // to find and use specific content type, check out this IANA page:
        // http://www.iana.org/assignments/media-types/media-types.xhtml

        if (array_key_exists($fileExtnesion, $content_types_list))
        {
            $contentType = $content_types_list[$fileExtnesion];
        }
        else
        {
            $contentType =  $default_contentType;
        }

        if(file_exists($filePath))
        {
            $size = filesize($filePath);
            $offset = 0;
            $length = $size;
            //HEADERS FOR PARTIAL DOWNLOAD FACILITY BEGINS
            if(isset($_SERVER['HTTP_RANGE']))
            {
                preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
                $offset = intval($matches[1]);
                $length = intval($matches[2]) - $offset;
                $fhandle = fopen($filePath, 'r');
                // seek to the requested offset, this is 0 if it's not a partial content request
                fseek($fhandle, $offset);
                $data = fread($fhandle, $length);
                fclose($fhandle);
                header('HTTP/1.1 206 Partial Content');
                header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $size);
            }

            //HEADERS FOR PARTIAL DOWNLOAD FACILITY BEGINS
            //USUAL HEADERS FOR DOWNLOAD
            header("Content-Disposition: attachment;filename={$fileName}");
            header("Content-Type: {$contentType}");
            header('Accept-Ranges: bytes');
            header('Pragma: public');
            header('Expires: -1');
            header('Cache-Control: no-cache');
            header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
            header('Content-Length: ' . filesize($filePath));
            $chunksize = 8 * (1024 * 1024); //8MB (highest possible fread length)

            if ($size > $chunksize)
            {
                $handle = fopen($_FILES['file']['tmp_name'], 'rb');
                $buffer = '';

                while (!feof($handle) && (connection_status() === CONNECTION_NORMAL))
                {
                    $buffer = fread($handle, $chunksize);
                    print $buffer;
                    ob_flush();
                    flush();
                }

              if(connection_status() !== CONNECTION_NORMAL)
              {
                echo 'Connection aborted';
              }
              fclose($handle);
            }
            else
            {
              ob_clean();
              flush();
              readfile($filePath);
            }
         }
         else
         {
           echo 'File does not exist!';
         }
    }
    else
    {
        echo 'There is no file to download!';
    }
}


/* Function to get correct MIME type for download */
function mimeTypes()
{
    /* Just add any required MIME type if you are going to download something not listed here.*/
    $mime_types = array('...'); 
    return $mime_types;                     
}