doume1301 2015-08-24 13:28
浏览 25
已采纳

如何从uploads文件夹下载一个图像,该文件夹放在保存下面程序的地方,php

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 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录