Use header definition as file and then ReadFile download (hide download address) _php Tutorial

Source: Internet
Author: User
Tags dbx fdf gcd pkcs12 pkcs7 rfc822 sendfile
Copy CodeThe 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 '];

$MD 5 = MD5 ($fileStat [' mtime ']. ' ='. $fileStat [' ino ']. ' ='. $fileStat [' size ']);
$etag = ' "'. $MD 5. '-' . CRC32 ($MD 5). '"';
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 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). ' "');//kanji automatically 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;
}
?>


The type of the attached server response HTTP ContentType Daquan:
". *" = "Application/octet-stream"
". 001" = "application/x-001"
". 301" = "application/x-301"
". 323" = "text/h323"
". 906" = "application/x-906"
". 907" = "drawing/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/*"
". CML" = "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" = "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"
". IGs" = "Application/x-igs"
". 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" = "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"
". MTx" = "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"
". Vsd" = "Application/vnd.visio"
". Vsd" = "Application/x-vsd"
". 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"

Reference:


Http://www.jb51.net/article/46439.htm

Http://www.jb51.net/article/20888.htm

http://www.bkjia.com/PHPjc/728083.html www.bkjia.com true http://www.bkjia.com/PHPjc/728083.html techarticle Copy the code as follows:? PHP function SendFile ($fileName, $fancyName = ", $forceDownload = true, $speedLimit = 0, $contentType = ' ') {if (!is_readable ($fileName)) {header ("HT ...

  • 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.