本文實例講述了php隱藏實際地址的文件下載方法。分享給大家供大家參考。具體如下:
下面這段php代碼可不透露實際的文件下載地址。
function download_document($filename,$path="",$mimetype="application/octet-stream") { header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Disposition: attachment; filename = $filename"); header("Content-Length: " . filesize($pathto . $filename)); header("Content-Type: $mimetype"); echo file_get_contents($pathto . $filename); }
實現方法二:
<?php $file = "1.txt";// 文件的真實地址(支持url,不過不建議用url) if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } ?>
希望本文所述對大家的php程序設計有所幫助。