php 檔案下載出錯
寫了一個 檔案下載的程式
PHP code
function download_file($filename){ //below to provide the download if (file_exists($filename)) { $file = fopen($filename); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($filename)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename) . ' bytes'); //ob_clean(); //flush(); readfile($filename); fclose($file); return true; }else{ echo("the file not exist"); return false; } //above to provide the download }
但是沒有進行 下載的功能。檔案在上一步已經產生了。 但是如果我將 return true改成exit 下載功能可以實現,但是檔案下載之後我還要進行其他的功能。請教大神們這是怎麼回事? 我看網上的下載例子也不是說需要exit才可以的
------解決方案--------------------
看你在哪調用這個函數,檢查下在哪裡調用的,那裡是不是有問題?
------解決方案--------------------
這樣呢:
PHP code
if(download_file($filename)){ #下面的操作}else{ #錯誤提示}
------解決方案--------------------
探討
寫了一個 檔案下載的程式PHP code
function download_file($filename){
//below to provide the download
if (file_exists($filename)) {
$file ……
------解決方案--------------------
探討
引用:
這樣呢:
PHP code
if(download_file($filename)){
#下面的操作
}else{
#錯誤提示
}
不太明白你說的 你說的也是將return給省略了嗎? return省略去了 我也不可以運行
------解決方案--------------------
你那個函數return false就相當於preventDefault不會提交表單
在滿足答件時,例如檢查欄位為非空,然後return true;頁面才進行跳轉,也才會調用你的action
這是表單提交常用的阻止異常提交的方法