downloading large sized file issue

135 views Asked by At

I have a download script that works well in my local machine . But after uploading to the server , when my ios app calls the script it stops after 100-120 seconds . I just changed the script into another server .It worked perfectly . I dont know what the configuration is missing in the server( the one having issue during download ) ? I googled alot , but still no solution . So my question is how can i solve the issue ? Whats the real problem ? One of my google search reached to the point of Xscend-file . But i have a doubt whether it support partial downloading ? My down script is..

$finfo         = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mime_type  = finfo_file($finfo, ROOT_PATH."/file_folder/".$file_name);
$file           = ROOT_PATH."/file_folder/".$file_name;             
$filesize       =   filesize($file);
$offset         =   0;
$length         =   $filesize;
if ( isset($_SERVER['HTTP_RANGE']) ) {

    $partialContent = true;
    preg_match('/bytes=(\d+)-?/', $_SERVER['HTTP_RANGE'], $matches);
    $offset = intval($matches[1]);
    $length = $filesize - $offset;
} 
else {
      $partialContent = false;
}
$file       = fopen($file, 'r');
fseek($file, $offset);
$data       = fread($file, $length);
fclose($file);
if ( $partialContent ) {

    header('HTTP/1.1 206 Partial Content');

    header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
}
//header("X-Sendfile: $file_name");
header('Content-Description: File Transfer');
header("Content-type: " .$mime_type);
header('Content-Disposition: attachment; filename="' . $file_name. '"');
//header('Content-Transfer-Encoding: binary');
    //header('Expires: 0');
    //header('Cache-Control: must-revalidate');
    //header('Pragma: public');
header('Content-Length: ' . $length);                   
header('Accept-Ranges: bytes');
print($data);
2

There are 2 answers

1
Satish Sharma On BEST ANSWER

try this code to download file

$filename = will be your file name with full path which you want to download

and

$filename2 = will be your file name after download the file

 header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.$filename2);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filename));
            ob_clean();
            flush();
            readfile($filename);
            exit;
1
Satish Sharma On

set Max execution time large on the server's php.ini file