php - 我将如何选择所有类型的附件,如 pdf、gif、jpg,以链接的形式并在单击链接时下载文件
问题描述
我可以选择并显示图像,但我需要将其显示为链接,然后单击链接下载
$imagesDirectory =basename("images/"); //path to directory
if (is_dir($imagesDirectory))
{
$opendirectory = opendir($imagesDirectory); // to read images from path
while (($image = readdir($opendirectory)) !== false)
{
if(($image == '.') || ($image == '..'))
{
continue;
}
$imgFileType = pathinfo($image,PATHINFO_EXTENSION);
if(($imgFileType == 'jpg') || ($imgFileType == 'png')) //Types of files
{
echo "<img src='images/".$image."' width='200'> ";
}
}
closedir($opendirectory);
}
?>
解决方案
您非常接近答案,只需将图像包装在锚标记中并将“下载”属性传递给它。而已:
$imagesDirectory =basename("images/"); //path to directory
if (is_dir($imagesDirectory))
{
$opendirectory = opendir($imagesDirectory); // to read images from path
while (($image = readdir($opendirectory)) !== false)
{
if(($image == '.') || ($image == '..'))
{
continue;
}
$imgFileType = pathinfo($image,PATHINFO_EXTENSION);
if(($imgFileType == 'jpg') || ($imgFileType == 'png')) //Types of files
{
/*xxxxxxxxxxxx-----Here's the Change-----xxxxxxxxxxxx*/
echo "<a href='images/".$image."' download><img src='images/".$image."' width='200'> </a>";
}
}
closedir($opendirectory);
}
?>
推荐阅读
- node.js - 在 Node.js 中的特定偏移量上将数组写入 Buffer
- javascript - 计算一个坐标是否在一串坐标中
- django - 如何在单击时渲染属于父模型的特定对象的子模型的所有对象?
- laravel - 使用 Laravel 作为 N 层架构
- python - 将背景颜色限制为 QTabBar 上的三角形选项卡
- css - 按钮未与 div 中的图像对齐
- javascript - 如何使用 JavaScript 存储文本值
- python - Django - 如何让电子邮件链接在移动设备上工作?
- sql - 数据库何时锁定数据?
- node.js - Firestore 查询 where(documentId(), 'in', array) 给我一个错误