2

i'm trying to create downloadable video-files. In my site there is a list of files. All videos are in .flv-format (flash). There is exact link to the file for the all videos. But in all browsers after clicking content is loading to the browser`s window. I needn't in this. As i understand i should create redirect-page wich contains mime-type of the download file. What exactly should i do? Language: php

Max Frai
  • 61,946
  • 78
  • 197
  • 306

2 Answers2

9

The recommended MIME type for that is application/octet-stream:

The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data. […]

The recommended action for an implementation that receives an "application/octet-stream" entity is to simply offer to put the data in a file, with any Content-Transfer-Encoding undone, or perhaps to use it as input to a user-specified process.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
7

Create a PHP page with the following:

<?php

$filepath = "path/to/file.ext";

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filepath");
header("Content-Type: mime/type");
header("Content-Transfer-Encoding: binary");
// UPDATE: Add the below line to show file size during download.
header('Content-Length: ' . filesize($filepath));

readfile($filepath);

?>

Set $filepath to the path of the file to be downloaded, and set Content-Type to the mime type of the file being downloaded.

Point the "download" link to this page.

For multiple files of the same type:

<?php

$filepath = $_GET['filepath'];

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filepath");
header("Content-Type: mime/type");
header("Content-Transfer-Encoding: binary");
// UPDATE: Add the below line to show file size during download.
header('Content-Length: ' . filesize($filepath));

readfile($filepath);

?>

Replace the information as specified above, and point the "download" link to this page with a GET parameter named "filepath" containing the file path.

For example, if you name this php file "download.php", point the download link for a file named "movie.mov" (in the same directory as download.php) to "download.php?filepath=movie.mov".

George Claghorn
  • 26,261
  • 3
  • 48
  • 48
  • As i know download.php?filepath=movie.mov isn't good =) Cause everyboady could download any file from server. But thanks anyway. – Max Frai Jul 02 '09 at 17:04
  • Alternatively, you could create one php file for each movie download file. But that could become tedious. – George Claghorn Jul 02 '09 at 17:34
  • Sorry, but your method isn't working =(. After going to the script i got a window from browser to download. It's that what i need, but it downloads file wich is 0 bytes =( – Max Frai Jul 02 '09 at 18:15
  • That's because the file size isn't specified. You can do this by adding another header: header('Content-Length: ' . filesize($filepath)); – George Claghorn Jul 02 '09 at 18:20
  • Sorry, it was my problem ;) Thanks for helping. – Max Frai Jul 02 '09 at 18:49