php下載有關問題,下載後檔案內容是空的,圖片也沒有預覽

來源:互聯網
上載者:User
php下載問題,下載後檔案內容是空的,圖片也沒有預覽
有兩個檔案,a.php 和 b.php
點擊a.php中的連結,開始對檔案下載:代碼如下

這是a.php

$file_dir = "D:/files";
$file_name = $row[1]; //這是從資料庫中尋找出來的
echo "" . $row[2] . ""; //點擊這個連結,下載的檔案名稱是對的,就是檔案裡沒有內容,如果是圖片,圖片沒有預覽
?>


這是b.php的全部代碼

$file_name = $_GET['file_name']; //檔案名稱
$file_dir = $_GET['file_dir']; //檔案路徑

if(!file_exists($file_dir.$file_name)){
echo "找不到 [" . $file_dir.$file_name . "] 檔案";
exit;
}else{
$file=fopen($file_dir.$file_name,"r");

Header("Content-type: application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Accept-Length:".filesize($file_dir.$file_name));
Header("Content-Disposition: attachment; filename=".$file_name);
}
?>

分享到:


------解決方案--------------------
a.php 中是 $file_dir = "D:/files";
那麼在 b.php 中的 $file_dir.$file_name 就缺少了路徑符

你在 b.php 中只有 $file=fopen($file_dir.$file_name,"r"); 開啟了檔案,但並沒有輸出檔案的內容

如果用 $file=fopen($file_dir.$file_name,"r"); 開啟的檔案是圖片的話,將不能正確讀出圖片資料
應使用二進位方式開啟:$file=fopen($file_dir.$file_name,"rb");
------解決方案--------------------
本帖最後由 xuzuning 於 2013-06-08 16:08:01 編輯

    }else{
$file = $file_dir.$file_name;
Header("Content-type: application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Accept-Length:".filesize($file));
Header("Content-Disposition: attachment; filename=".$file_name);
readfile($file);
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.