I have saved an image which is in upload folder, the link is saved in sql table, how should i write a program to download the images from that folder?
1条回答 默认 最新
- dongzuan4860 2015-08-24 14:06关注
for database connection try these codes..
database.php
<?php try{ $db = new PDO('mysql:host=localhost;dbname=database_name', "username" , "password"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ print "error in connection" . $e->getMessage(); }
now,this php file will access the name of image file stored in database image.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <?php require_once 'database.php'; $stmt = $db->query("SELECT image_name FROM tablename"); $row = $stmt->fetchall(PDO::FETCH_ASSOC); foreach ($row as $value) { $filename = $value['image_name']; ?> <a href="download.php?id=<?php echo $filename;?>"><?php echo $filename . "<br>";?></a> <?php }?> </body> </html>
download.php
<?php $filename = $_GET["id"]; header('Content-Type: image/jpg'); header('Content-Disposition: attachment; filename="' . basename($filename) . '";'); readfile ($filename); exit;
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
- ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
- ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
- ¥15 python天天向上类似问题,但没有清零
- ¥30 3天&7天&&15天&销量如何统计同一行
- ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
- ¥15 C#调用python代码(python带有库)
- ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?