PHP下載文件代碼【可自命名】

jopen 9年前發布 | 786 次閱讀 PHP

采用get方式傳入參數http://domain/download.php?file='savepath on server'&name='file name you want'

<?php //傳入文件路徑和文件名,分別用于讀取文件和重命名文件 if( empty($_GET['file']) || empty($_GET['name'])){ echo'<script> alert("找不到文件了..... !"); location.replace ("index.php") </script>'; exit(); } //文件路徑 $file=$_GET['file']; //文件名【即文件的標題,而非服務器上保存的名字】 $name=$_GET['name']; //獲取文件后綴 $suffix = substr(strrchr($file, '.'), 1);

if(is_file('./'.$file)) { header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=".$name.'.'.$suffix); readfile('./'.$file); exit; }else{ echo "文件不存在!"; exit; } ?></pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!