Please refer to the question of downloading MP4 files in PHP. the file download is incomplete.

Source: Internet
Author: User
Please refer to the question of downloading MP4 files in PHP. the file download is incomplete.
function dl_file($file){    $len = filesize($file);    $filename = basename($file);    $file_extension = strtolower(substr(strrchr($filename,"."),1));    switch( $file_extension ) {    case "mp4": $ctype="video/mp4"; break;    default: $ctype="application/force-download";    }    header("Pragma: public");    header("Expires: 0");    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");    header("Cache-Control: public");     header("Content-Description: File Transfer");    header("Content-Type: $ctype");    $header="Content-Disposition: attachment; filename=".$filename."";    header($header );    header("Content-Transfer-Encoding: binary");    header("Content-Length: ".$len);ob_clean();    flush();    readfile($file);    exit;}dl_file('test.mp4');


Google Chrome and IE are not complete.


Reply to discussion (solution)

Ob_clean ();
Flush ();
Readfile ($ file );

Ob_clean ();
Flush ();
Readfile ($ file );


This sentence exists.

No mime header added

Header ("Content-Transfer-Encoding: binary ");
Header ("Content-Length:". $ len );
Delete them.

No mime header added



MIME type? Canadian, IIS?

Header ("Content-Transfer-Encoding: binary ");
Header ("Content-Length:". $ len );
Delete them.





$ Len = filesize ($ file );
Header ("Content-Length:". $ len );
The preceding two sentences can be downloaded after deletion, but the file size is not counted. the download duration is not displayed for thunder and IE.

In the nginx + php environment, without the problem you mentioned, both ie and Thunder can download my files normally, but my files are flv. in this case, it can only indicate that the mime header is incorrect.
My code is as follows:

Function dl_file ($ file ){
$ Len = filesize ($ file );
$ Filename = basename ($ file );
$ File_extension = strtolower (substr (strrchr ($ filename, "."), 1 ));
Switch ($ file_extension ){
Case "mp4": $ ctype = "video/mp4"; break;
Default: $ ctype = "application/force-download ";
}
Header ("Pragma: public ");
Header ("Expires: 0 ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Cache-Control: public ");
Header ("Content-Description: File Transfer ");
Header ("Content-Type: $ ctype ");
$ Header = "Content-Disposition: attachment; filename =". $ filename ."";
Header ($ header );
Header ("Content-Transfer-Encoding: binary ");
Header ("Content-Length:". $ len );
Ob_clean ();
Flush ();
Readfile ($ file );
Exit;
}
Dl_file ('http: // res.805.com/videos/huoshan Shanglin Houyuan (yi group) [640x480]. flv '); only one file name is changed, but none of the other files have been changed.

In the nginx + php environment, without the problem you mentioned, both ie and Thunder can download my files normally, but my files are flv. in this case, it can only indicate that the mime header is incorrect.
My code is as follows:

Function dl_file ($ file ){
$ Len = filesize ($ file );
$ Filename = basename ($ file );
$ File_extension = strtolower (substr (strrchr ($ filename, "."), 1 ));
Switch ($ file_extension ){
Case "mp4": $ ctype = "video/mp4"; break;
Default: $ ctype = "application/force-download ";
}
Header ("Pragma: public ");
Header ("Expires: 0 ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Cache-Control: public ");
Header ("Content-Description: File Transfer ");
Header ("Content-Type: $ ctype ");
$ Header = "Content-Disposition: attachment; filename =". $ filename ."";
Header ($ header );
Header ("Content-Transfer-Encoding: binary ");
Header ("Content-Length:". $ len );
Ob_clean ();
Flush ();
Readfile ($ file );
Exit;
}
Dl_file ('http: // res.805.com/videos/huoshan Shanglin Houyuan (yi group) [640x480]. flv '); only one file name is changed, but none of the other files have been changed.



Is the file size displayed during IE and Thunder downloads?

I have never tested it. There should be a Content-Length header.

You can go to the ucenter documentation.

Blog viewing
Http://www.jb51.net/article/46440.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.