0

Helllo there..

The following code simply reads out myfile.mp4 and serves it to the client:

$path = "myfile.mp4";

header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type($path));
header('Content-Disposition: inline; filename="'.basename($path).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
readfile($path); 
exit;

Sadly, with files over 3GB (from what I observed), mp4 files are really hard to read.

Here is the list of problems:

1) You cant seek to the end of the video. readfile() will only serve your files "linearly" i'd say...

2) It takes an awful amount of time to load compared to direct, vanilla file access.

I tried looking into both the vanilla file access headers and mines and this is what I got:

== Vanilla apache2 file serving ==
Cause: Document

HTTP/1.1 200 OK
Date: Fri, 13 Dec 2019 05:55:42 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Thu, 12 Dec 2019 18:51:19 GMT
ETag: "9800d27f-599863b11b568"
Accept-Ranges: bytes
Content-Length: 2550190719
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: video/mp4


Cause: Media
HTTP/1.1 206 Partial Content
Date: Fri, 13 Dec 2019 05:58:29 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Thu, 12 Dec 2019 18:51:19 GMT
ETag: "9800d27f-599863b11b568"
Accept-Ranges: bytes
Content-Length: 9032319
Content-Range: bytes 2541158400-2550190718/2550190719
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: video/mp4

And here's my php script's headers (It took me 4 minutes to get them!!!)

Cause: Document
HTTP/1.1 200 OK
Date: Fri, 13 Dec 2019 05:57:36 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Description: File Transfer
Content-Disposition: inline; filename=" - 20191126(ç«) 2330éå§ - ãã³ãã³çæ¾é.mp4"
Expires: 0
Cache-Control: must-revalidate
Pragma: public
Content-Length: 2550190719
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: video/mp4

I also have a "Cause: media" request apparently on this page but I didnt receive any headers from the server..

What am I doing wrong? Is there a better function to do this? Preferably a function that also let me transfer JPEG files and stuff like that...

Thanks for the read everyone!!

keanu_reeves
  • 350
  • 1
  • 12
  • Maybe this previous answer will help you: https://stackoverflow.com/a/42372831/2061685 – Leo Melin Dec 13 '19 at 06:12
  • @LeoMelin The code you linked forces the client to download the content. I'll try and set the headers to something else.. – keanu_reeves Dec 13 '19 at 12:46
  • @LeoMelin Even with the headers set as something else, this code you linked only distributes files in a linear way.. I cant seek to the end of videos, for example. Also, big videos still takes alot of time to load! – keanu_reeves Dec 14 '19 at 14:53

1 Answers1

1

Have you tried sending the file in chunks? Also you should set the correct header like:

header("Content-Type: application/octet-stream");

Like this:

set_time_limit(0);
$file = @fopen($file_path,"rb");
while(!feof($file))
{
    print(@fread($file, 1024*8));
    ob_flush();
    flush();
}

Also, if you are on Apache server, you should disable output compression & output buffering by adding a .htaccess file with:

SetEnv no-gzip dont-vary

Does this help at all?

Leo Melin
  • 234
  • 2
  • 6
  • The video loads are slightly faster (around 30 seconds), but I still cant skip to the end of a video... Sorry for the late reply! -- By directly accessing the file, the video loads in under a second. – keanu_reeves Dec 30 '19 at 00:47