BobbyP
BobbyP

Reputation: 2265

How to download a very large video file instead of playing in browser with PHP

I have created a movie which I have saved as an MP4 file and uploaded to my server. The file is 4.6 GB. When I have tried to send a link to my family, the video tries to play in their browser.

What I want to do is have them click a link and the file downloads to their computer instead. I have searched endlessly for solutions, but they all keep failing, probably due to the size of the file.

Is anyone able to help with a PHP script that will allow the downloading of such a large file?

Thanks

Upvotes: 1

Views: 3636

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

The easiest solution is to press Ctrl+S, select File>Save or do right click + Save as in the browser when the file starts to load - this will open the Save File dialog.

In case you want to return this file from PHP, you can do that with following script:

<?php
$file = 'video.mp4';

if (file_exists($file)) {
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename='.basename($file));
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  readfile($file);
  exit;

}

Upvotes: 2

Related Questions