[英]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.