Sometimes, to hide the truth, we define the file as a header and then download the readfile, but this will increase the burden on the server. Generally, we do not recommend downloading large files.
Sometimes, to hide the truth, we define the file as a header and then download the readfile, but this will increase the burden on the server. Generally, we do not recommend downloading large files.
The Code is as follows:
Function sendFile ($ fileName, $ fancyName = '', $ forceDownload = true, $ speedLimit = 0, $ contentType = '')
{
If (! Is_readable ($ fileName ))
{
Header ("HTTP/1.1 404 Not Found ");
Return false;
}
$ FileStat = stat ($ fileName );
$ LastModified = $ fileStat ['mtime'];
$ Md5 = md5 ($ fileStat ['mtime']. '='. $ fileStat ['ino '].' = '. $ fileStat ['SIZE']);
$ Etag = '"'. $ md5. '-'. crc32 ($ md5 ).'"';
Header ('Last-Modified: '. gmdate ("D, d m y h: I: s", $ lastModified). 'gmt ');
Header ("ETag: $ etag ");
If (isset ($ _ SERVER ['HTTP _ IF_MODIFIED_SINCE ']) & strtotime ($ _ SERVER ['HTTP _ IF_MODIFIED_SINCE'])> = $ lastModified)
{
Header ("HTTP/1.1 304 Not Modified ");
Return true;
}
If (isset ($ _ SERVER ['HTTP _ IF_UNMODIFIED_SINCE ']) & strtotime ($ _ SERVER ['HTTP _ IF_UNMODIFIED_SINCE']) <$ lastModified)
{
Header ("HTTP/1.1 304 Not Modified ");
Return true;
}
If (isset ($ _ SERVER ['HTTP _ IF_NONE_MATCH ']) & $ _ SERVER ['HTTP _ IF_NONE_MATCH'] = $ etag)
{
Header ("HTTP/1.1 304 Not Modified ");
Return true;
}
If ($ fancyName = '')
{
$ FancyName = basename ($ fileName );
}
If ($ contentType = '')
{
$ ContentType = 'application/octet-stream ';
}
$ FileSize = $ fileStat ['SIZE'];
$ ContentLength = $ fileSize;
$ IsPartial = false;
If (isset ($ _ SERVER ['HTTP _ range'])
{
If (preg_match ('/^ bytes = (d *)-(d *) $/', $ _ SERVER ['HTTP _ range'], $ matches ))
{
$ StartPos = $ matches [1];
$ EndPos = $ matches [2];
If ($ startPos = ''& $ endPos = '')
{
Return false;
}
If ($ startPos = '')
{
$ StartPos = $ fileSize-$ endPos;
$ EndPos = $ fileSize-1;
}
Else if ($ endPos = '')
{
$ EndPos = $ fileSize-1;
}
$ StartPos = $ startPos <0? 0: $ startPos;
$ EndPos = $ endPos> $ fileSize-1? $ FileSize-1: $ endPos;
$ Length = $ endPos-$ startPos + 1;
If ($ length <0)
{
Return false;
}
$ ContentLength = $ length;
$ IsPartial = true;
}
}
// Send headers
If ($ isPartial)
{
Header ('HTTP/1.1 206 Partial content ');
Header ("Content-Range: bytes $ startPos-$ endPos/$ fileSize ");
}
Else
{
Header ("HTTP/1.1 200 OK ");
$ StartPos = 0;
$ EndPos = $ contentLength-1;
}
Header ('pragma: cache ');
Header ('cache-Control: public, must-revalidate, max-age = 0 ');
Header ('Accept-Ranges: bytes ');
Header ('content-type: '. $ contentType );
Header ('content-Length: '. $ contentLength );
If ($ forceDownload)
{
Header ('content-Disposition: attachment; filename = "'. rawurlencode ($ fancyName).'" '); // automatically convert Chinese characters to URL Encoding
Header ('content-Disposition: attachment; filename = "'. $ fancyName .'"');
}
Header ("Content-Transfer-Encoding: binary ");
$ BufferSize = 2048;
If ($ speedLimit! = 0)
{
$ PacketTime = floor ($ bufferSize * 1000000/$ speedLimit );
}
$ BytesSent = 0;
$ Fp = fopen ($ fileName, "rb ");
Fseek ($ fp, $ startPos );
// Fpassthru ($ fp );
While ($ bytesSent <$ contentLength &&! Feof ($ fp) & connection_status () = 0)
{
If ($ speedLimit! = 0)
{
List ($ usec, $ sec) = explode ("", microtime ());
$ OutputTimeStart = (float) $ usec + (float) $ sec );
}
$ ReadBufferSize = $ contentLength-$ bytesSent <$ bufferSize? $ ContentLength-$ bytesSent: $ bufferSize;
$ Buffer = fread ($ fp, $ readBufferSize );
Echo $ buffer;
Ob_flush ();
Flush ();
$ BytesSent + = $ readBufferSize;
If ($ speedLimit! = 0)
{
List ($ usec, $ sec) = explode ("", microtime ());
$ OutputTimeEnd = (float) $ usec + (float) $ sec );
$ UseTime = (float) $ outputTimeEnd-(float) $ outputTimeStart) * 1000000;
$ SleepTime = round ($ packetTime-$ useTime );
If ($ sleepTime> 0)
{
Usleep ($ sleepTime );
}
}
}
Return true;
}
?>
Appendix Server Response HTTP type ContentType Daquan:
". *" = "Application/octet-stream"
". 001" = "application/x-001"
". 301" = "application/x-301"
". 323" = "text/h323"
". 906" = "application/x-906"
". 907" = "Fig/907"
". A11" = "application/x-a11"
". Acp" = "audio/x-mei-aac"
". Ai" = "application/postscript"
". Aif" = "audio/aiff"
". Aifc" = "audio/aiff"
". Aiff" = "audio/aiff"
". Anv" = "application/x-anv"
". Asa" = "text/asa"
". Asf" = "video/x-ms-asf"
". Asp" = "text/asp"
". Asx" = "video/x-ms-asf"
". Au" = "audio/basic"
". Avi" = "video/avi"
". Awf" = "application/vnd. adobe. workflow"
". Biz" = "text/xml"
". Bmp" = "application/x-bmp"
". Bot" = "application/x-bot"
". C4t" = "application/x-c4t"
". C90" = "application/x-c90"
". Cal" = "application/x-cals"
". Cat" = "application/vnd. ms-pki.seccat"
". Cdf" = "application/x-netcdf"
". Cdr" = "application/x-cdr"
". Cel" = "application/x-cel"
". Cer" = "application/x-x509-ca-cert"
". Cg4" = "application/x-g4"
". Cgm" = "application/x-cgm"
". Cit" = "application/x-cit"
". Class" = "java /*"
". CRF" = "text/xml"
". Cmp" = "application/x-cmp"
". Cmx" = "application/x-cmx"
". Cot" = "application/x-cot"
". Crl" = "application/pkix-crl"
". Crt" = "application/x-x509-ca-cert"
". Csi" = "application/x-csi"
". Css? 6.1.3 "=" text/css"
". Cut" = "application/x-cut"
". Dbf" = "application/x-dbf"
". Dbm" = "application/x-dbm"
". Dbx" = "application/x-dbx"
". Dcd" = "text/xml"
". Dcx" = "application/x-dcx"
". Der" = "application/x-x509-ca-cert"
". Dgn" = "application/x-dgn"
". Dib" = "application/x-dib"
". Dll" = "application/x-msdownload"
". Doc" = "application/msword"
". Dot" = "application/msword"
". Drw" = "application/x-drw"
". Dtd" = "text/xml"
". Dwf" = "Model/vnd. dwf"
". Dwf" = "application/x-dwf"
". Dwg" = "application/x-dwg"
". Dxb" = "application/x-dxb"
". Dxf" = "application/x-dxf"
". Edn" = "application/vnd. adobe. edn"
". Emf" = "application/x-emf"
". Eml" = "message/rfc822"
". Ent" = "text/xml"
". Epi" = "application/x-epi"
". Eps" = "application/x-ps"
". Eps" = "application/postscript"
". Etd" = "application/x-ebx"
". Exe" = "application/x-msdownload"
". Fax" = "image/fax"
". Fdf" = "application/vnd. fdf"
". Fif" = "application/fractals"
". Fo" = "text/xml"
". Frm" = "application/x-frm"
". G4" = "application/x-g4"
". Gbr" = "application/x-gbr"
". Gcd" = "application/x-gcd"
". Gif" = "image/gif"
". Gl2" = "application/x-gl2"
". Gp4" = "application/x-gp4"
". Hgl" = "application/x-hgl"
". Hmr" = "application/x-hmr"
". Hpg" = "application/x-hpgl"
". Hpl" = "application/x-hpl"
". Hqx" = "application/mac-binhex40"
". Hrf" = "application/x-hrf"
". Hta" = "application/hta"
". Htc" = "text/x-component"
". Htm" = "text/html"
". Html" = "text/html"
". Htt" = "text/webviewhtml"
". Htx" = "text/html"
". Icb" = "application/x-icb"
". Ico" = "image/x-icon"
". Ico" = "application/x-ico"
". Iff" = "application/x-iff"
". Ig4" = "application/x-g4"
". Ig" = "application/x-ig"
". Iii" = "application/x-iphone"
". Img" = "application/x-img"
". Ins" = "application/x-internet-signup"
". Isp" = "application/x-internet-signup"
". IVF" = "video/x-ivf"
". Java" = "java /*"
". Jfif" = "image/jpeg"
". Jpe" = "image/jpeg"
". Jpe" = "application/x-jpe"
". Jpeg" = "image/jpeg"
". Jpg" = "image/jpeg"
". Jpg" = "application/x-jpg"
". Js? 6.1.3 "=" application/x-javascript"
". Jsp" = "text/html"
". La1" = "audio/x-liquid-file"
". Lar" = "application/x-laplayer-reg"
". Latex" = "application/x-latex"
". Lavs" = "audio/x-liquid-secure"
". Lbm" = "application/x-lbm"
". Lmsff" = "audio/x-la-lms"
". Ls" = "application/x-javascript"
". Ltr" = "application/x-ltr"
". M1v" = "video/x-mpeg"
". M2v" = "video/x-mpeg"
". M3u" = "audio/mpegurl"
". M4e" = "video/mpeg4"
". Mac" = "application/x-mac"
". Man" = "application/x-troff-man"
". Math" = "text/xml"
". Mdb" = "application/msaccess"
". Mdb" = "application/x-mdb"
". Mfp" = "application/x-shockwave-flash"
". Mht" = "message/rfc822"
". Mhtml" = "message/rfc822"
". Mi" = "application/x-mi"
". Mid" = "audio/mid"
". Midi" = "audio/mid"
". Mil" = "application/x-mil"
". Mml" = "text/xml"
". Mnd" = "audio/x-musicnet-download"
". Mns" = "audio/x-musicnet-stream"
". Mocha" = "application/x-javascript"
". Movie" = "video/x-sgi-movie"
". Mp1" = "audio/mp1"
". Mp2" = "audio/mp2"
". Mp2v" = "video/mpeg"
". Mp3" = "audio/mp3"
". Mp4" = "video/mpeg4"
". Mpa" = "video/x-mpg"
". Mpd" = "application/vnd. ms-project"
". Mpe" = "video/x-mpeg"
". Mpeg" = "video/mpg"
". Mpg" = "video/mpg"
". Mpga" = "audio/rn-mpeg"
". Mpp" = "application/vnd. ms-project"
". Mps" = "video/x-mpeg"
". Mpt" = "application/vnd. ms-project"
". Mpv" = "video/mpg"
". Mpv2" = "video/mpeg"
". Mpw" = "application/vnd. ms-project"
". Mpx" = "application/vnd. ms-project"
". CTX" = "text/xml"
". Mxp" = "application/x-mmxp"
". Net" = "image/pnetvue"
". Nrf" = "application/x-nrf"
". Nws" = "message/rfc822"
". Odc" = "text/x-ms-odc"
". Out" = "application/x-out"
". P10" = "application/pkcs10"
". P12" = "application/x-pkcs12"
". P7b" = "application/x-pkcs7-certificates"
". P7c" = "application/pkcs7-mime"
". P7m" = "application/pkcs7-mime"
". P7r" = "application/x-pkcs7-certreqresp"
". P7s" = "application/pkcs7-signature"
". Pc5" = "application/x-pc5"
". Pci" = "application/x-pci"
". Pcl" = "application/x-pcl"
". Pcx" = "application/x-pcx"
". Pdf" = "application/pdf"
". Pdf" = "application/pdf"
". Pdx" = "application/vnd. adobe. pdx"
". Pfx" = "application/x-pkcs12"
". Pgl" = "application/x-pgl"
". Pic" = "application/x-pic"
". Pko" = "application/vnd. ms-pki.pko"
". Pl" = "application/x-perl"
". Plg" = "text/html"
". Pls" = "audio/scpls"
". Plt" = "application/x-plt"
". Png" = "image/png"
". Png" = "application/x-png"
". Pot" = "application/vnd. ms-powerpoint"
". Ppa" = "application/vnd. ms-powerpoint"
". Ppm" = "application/x-ppm"
". Pps" = "application/vnd. ms-powerpoint"
". Ppt" = "application/vnd. ms-powerpoint"
". Ppt" = "application/x-ppt"
". Pr" = "application/x-pr"
". Prf" = "application/pics-rules"
". Prn" = "application/x-prn"
". Prt" = "application/x-prt"
". Ps" = "application/x-ps"
". Ps" = "application/postscript"
". Ptn" = "application/x-ptn"
". Pwz" = "application/vnd. ms-powerpoint"
". R3t" = "text/vnd. rn-realtext3d"
". Ra" = "audio/vnd. rn-realaudio"
". Ram" = "audio/x-pn-realaudio"
". Ras" = "application/x-ras"
". Rat" = "application/rat-file"
". Rdf" = "text/xml"
". Rec" = "application/vnd. rn-recording"
". Red" = "application/x-red"
". Rgb" = "application/x-rgb"
". Rjs" = "application/vnd. rn-realsystem-rjs"
". Rjt" = "application/vnd. rn-realsystem-rjt"
". Rlc" = "application/x-rlc"
". Rle" = "application/x-rle"
". Rm" = "application/vnd. rn-realmedia"
". Rmf" = "application/vnd. adobe. rmf"
". Rmi" = "audio/mid"
". Rmj" = "application/vnd. rn-realsystem-rmj"
". Rmm" = "audio/x-pn-realaudio"
". Rmp" = "application/vnd. rn-rn_music_package"
". Rms" = "application/vnd. rn-realmedia-secure"
". Rmvb" = "application/vnd. rn-realmedia-vbr"
". Rmx" = "application/vnd. rn-realsystem-rmx"
". Rnx" = "application/vnd. rn-realplayer"
". Rp" = "image/vnd. rn-realpix"
". Rpm" = "audio/x-pn-realaudio-plugin"
". Rsml" = "application/vnd. rn-rsml"
". Rt" = "text/vnd. rn-realtext"
". Rtf" = "application/msword"
". Rtf" = "application/x-rtf"
". Rv" = "video/vnd. rn-realvideo"
". Sam" = "application/x-sam"
". Sat" = "application/x-sat"
". Sdp" = "application/sdp"
". Sdw" = "application/x-sdw"
". Sit" = "application/x-stuffit"
". Slb" = "application/x-slb"
". Sld" = "application/x-sld"
". Slk" = "drawing/x-slk"
". Smi" = "application/smil"
". Smil" = "application/smil"
". Smk" = "application/x-smk"
". Snd" = "audio/basic"
". Sol" = "text/plain"
". Sor" = "text/plain"
". Spc" = "application/x-pkcs7-certificates"
". Spl" = "application/futuresplash"
". Spp" = "text/xml"
". Ssm" = "application/streamingmedia"
". Sst" = "application/vnd. ms-pki.certstore"
". Stl" = "application/vnd. ms-pki.stl"
". Stm" = "text/html"
". Sty" = "application/x-sty"
". Svg" = "text/xml"
". Swf" = "application/x-shockwave-flash"
". Tdf" = "application/x-tdf"
". Tg4" = "application/x-tg4"
". Tga" = "application/x-tga"
". Tif" = "image/tiff"
". Tif" = "application/x-tif"
". Tiff" = "image/tiff"
". Tld" = "text/xml"
". Top" = "drawing/x-top"
". Torrent" = "application/x-bittorrent"
". Tsd" = "text/xml"
". Txt" = "text/plain"
". Uin" = "application/x-icq"
". Uls" = "text/iuls"
". Vcf" = "text/x-vcard"
". Vda" = "application/x-vda"
". Vdx" = "application/vnd. visio"
". Vml" = "text/xml"
". Vpg" = "application/x-vpeg005"
". Sealing" = "application/vnd. visio"
". Sealing" = "application/x-sealing"
". Vss" = "application/vnd. visio"
". Vst" = "application/vnd. visio"
". Vst" = "application/x-vst"
". Vsw" = "application/vnd. visio"
". Vsx" = "application/vnd. visio"
". Vtx" = "application/vnd. visio"
". Vxml" = "text/xml"
". Wav" = "audio/wav"
". Wax" = "audio/x-ms-wax"
". Wb1" = "application/x-wb1"
". Wb2" = "application/x-wb2"
". Wb3" = "application/x-wb3"
". Wbmp" = "image/vnd. wap. wbmp"
". Wiz" = "application/msword"
". Wk3" = "application/x-wk3"
". Wk4" = "application/x-wk4"
". Wkq" = "application/x-wkq"
". Wks" = "application/x-wks"
". Wm" = "video/x-ms-wm"
". Wma" = "audio/x-ms-wma"
". Wmd" = "application/x-ms-wmd"
". Wmf" = "application/x-wmf"
". Wml" = "text/vnd. wap. wml"
". Wmv" = "video/x-ms-wmv"
". Wmx" = "video/x-ms-wmx"
". Wmz" = "application/x-ms-wmz"
". Wp6" = "application/x-wp6"
". Wpd" = "application/x-wpd"
". Wpg" = "application/x-wpg"
". Wpl" = "application/vnd. ms-wpl"
". Wq1" = "application/x-wq1"
". Wr1" = "application/x-wr1"
". Wri" = "application/x-wri"
". Wrk" = "application/x-wrk"
". Ws" = "application/x-ws"
". Ws2" = "application/x-ws"
". Wsc" = "text/scriptlet"
". Wsdl" = "text/xml"
". Wvx" = "video/x-ms-wvx"
". Xdp" = "application/vnd. adobe. xdp"
". Xdr" = "text/xml"
". Xfd" = "application/vnd. adobe. xfd"
". Xfdf" = "application/vnd. adobe. xfdf"
". Xhtml" = "text/html"
". Xls" = "application/vnd. ms-excel"
". Xls" = "application/x-xls"
". Xlw" = "application/x-xlw"
". Xml" = "text/xml"
". Xpl" = "audio/scpls"
". Xq" = "text/xml"
". Xql" = "text/xml"
". Xquery" = "text/xml"
". Xsd" = "text/xml"
". Xsl" = "text/xml"
". Xslt" = "text/xml"
". Xwd" = "application/x-xwd"
". X_ B" = "application/x-x_ B"
". X_t" = "application/x-x_t"
Refer:
,