-2

I am trying to make a php script when you load on to that page it downloads a video. However since i do not know anything about headers it seems i can't figure it out so please explain why it works and how header works. I am trying to make the browser download video files.Can someone also explaain the headers and what they do please.

Here is my failing code:

<?php 
//Outputing video name
    $file_name = $_POST['FileToD'];
//outputting video extension e.g video/mp4
    $file_ext= $_POST['FileExt'];
//where the file is kept
    $file_path = 'mysever.myadress.com/media/movies/' . $file_name;
    header('Content-Type:'.$file_ext);
    header('Content-Length:' . filesize($file_path));
    header('Content-Description: attachment; filename='.$file_name);
    readfile($file_path);
?>
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Asim Poptani
  • 158
  • 1
  • 14

1 Answers1

4
  1. If you want to output a video, then don't start by outputting HTML and then switch to video data as part of the same file. (You can't set response headers after you've started outputting data anyway). Remove everything before <?php and after ?>
  2. $file_url should be the path, on the server's file system, to the file you want to make available. It shouldn't be a URL (unless you want a really inefficient approach or need to proxy from a different server), and if it is a URL then it needs to start with the scheme (e.g. http://).
  3. The content-type needs to be the actual content type of the video (e.g. video/mp4), not a file extension (and it doesn't make sense for it to be provided by the user).

You also need to sanitise the user data. At present (if the errors described above were fixed) then anybody could request any file that exists on the server.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335