1

I am trying to download a large file using the script below. The file downloads, but its named 'download' and the file extension is missing. How can I modify the code below so that the original file name and extension is preserved ? Also is there anyway to automatically detect the mime type and include that as well ?

Thanks a lot in advance.

       $path = 'public/Uploads/Films/files/Crank2006.avi';


    $size=filesize($path);

    $fm=@fopen($path,'rb');
    if(!$fm) {
      // You can also redirect here
      header ("HTTP/1.0 404 Not Found");
      die();
    }

    $begin=0;
    $end=$size;

    if(isset($_SERVER['HTTP_RANGE'])) {
      if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
        $begin=intval($matches[0]);
        if(!empty($matches[1])) {
          $end=intval($matches[1]);
        }
      }
    }

    if($begin>0||$end<$size)
      header('HTTP/1.0 206 Partial Content');
    else
      header('HTTP/1.0 200 OK');

    header("Content-Type: video/avi");
    header('Accept-Ranges: bytes');
    header('Content-Length:'.($end-$begin));
    header("Content-Disposition: inline;");
    header("Content-Range: bytes $begin-$end/$size");
    header("Content-Transfer-Encoding: binary\n");
    header('Connection: close');

    $cur=$begin;
    fseek($fm,$begin,0);

    while(!feof($fm)&&$cur<$end&&(connection_status()==0))
    { print fread($fm,min(1024*16,$end-$cur));
      $cur+=1024*16;
      usleep(1000);
    }
    die();
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
stooicrealism
  • 548
  • 2
  • 9
  • 29
  • possible duplicate of [Download File to server from URL](http://stackoverflow.com/questions/3938534/download-file-to-server-from-url) – Peon Jan 02 '14 at 12:30
  • "Fix my code" (that downloads movies) - vote to close. "Your Code" is a straight copy/paste from http://stackoverflow.com/a/18256636/46675, http://stackoverflow.com/q/16732419/46675, https://discussion.dreamhost.com/archive/index.php?thread-128795.html, and about 15 other pages where 'script hunters' keep copying/pasting bad code. Finding something from hotscripts and then posting questions about doesn't meet the minimum requirements in the FAQ. – Mike B Jan 02 '14 at 12:39
  • Please do not edit an answer into your question. If you have an answer not already covered below, post your own. – Andrew Barber Jan 03 '14 at 17:48

3 Answers3

1

In Content Disposition header you need to specify file name

$file_url = 'what you want to set'
header("Content-disposition: attachment; filename=\"" . $file_url. "\"");

A good tutorial on forced download php here.

For mime type see the following SO post

Community
  • 1
  • 1
chanchal118
  • 3,551
  • 2
  • 26
  • 52
0

Just do this below the $path

$path = 'public/Uploads/Films/files/Crank2006.avi';
$filename = array_pop(explode('/',$path)); // Grabbing the filename ... it will be Crank2006.avi

and add the header with filename to your existing headers.

header("Content-disposition: filename=$filename");

EDIT:

Detecting MIME type...

$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • Thanks, this works. But how do I get the mime type from file ? – stooicrealism Jan 02 '14 at 12:33
  • 1
    He's a script hunter - he's not here to understand anything. His final solution was to use an entirely different script he found somewhere. His comment about getting mime types, when typed directly into the search bar yields an answer. Don't encourage him. He tricks people here by copying/pasting code from somewhere else to satisfy the people who say "What have you tried?", "Show your code". Don't fall for it.. these are simply moderately more intelligent help vampires. – Mike B Jan 02 '14 at 13:22
0

Try Something Like Below

$file='test.pdf' //File to download with Large Size
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile('backup/'.$file);
    return 1;
} else {
    return 0;
}
Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Gopal Joshi
  • 2,350
  • 22
  • 49