[英]PHP & HTML: Download file link not working
以下代碼列出了本地計算機中某個文件夾中包含的所有文件,如您所見,我正在正確使用href屬性回顯標記內的文件路徑。
因此,問題在於,當我單擊標記時,除非將鏈接復制並粘貼到瀏覽器的另一個選項卡中,否則不會帶我去下載文件,為什么會發生這種情況? 我該如何解決?
<h5>Attached Files</h5>
<?php
$date = $dateCreated->format('d-m-Y');
$thepath = public_path().'/uploads/binnacle/'.$date.'/'.$activity->activity_id;
$handle = opendir($thepath);
$x = 1;
while(($file = readdir($handle))!= FALSE) {
if($file != "." && $file != ".." && $file != "thumbs" && $file != "thumbs.db") {
echo $x.".- "."<a href='$thepath/$file' target='_blank'>$file</a><br>";
$x++;
}
}
closedir($handle);
?>
注意:這會發生在所有文件類型上,包括圖像,excel文件,文本文檔等。
@WereWolf提供的解決方案-Alpha:
<?php
$date = $dateCreated->format('d-m-Y');
$thepath = "/uploads/binnacle/".$date."/".$activity->activity_id;
$handle = opendir(public_path().$thepath);
$x = 1;
while(($file = readdir($handle))!= FALSE) {
if($file != "." && $file != ".." && $file != "thumbs" && $file != "thumbs.db")
{
echo $x.'.- '.'<a href="'.asset($thepath.'/'.$file).'" target="_blank">'.$file.'</a><br>';
$x++;
}
}
closedir($handle);
?>
進行一些更改:
// Remove the public_path()
$thepath = 'uploads/binnacle/'.$date.'/'.$activity->activity_id;
然后在鏈接中:
"<a href='" . asset($thepath/$file) . "' target='_blank'>$file</a><br>";
注意: Laravel
有一個File
組件,您可以使用它,在source中檢查它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.