首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像下载功能

图像下载功能
EN

Stack Overflow用户
提问于 2013-07-14 10:37:26
回答 1查看 615关注 0票数 0

所以我做了一个图片托管网站,我试着添加一个下载图片的选项。我有一个s脚本,它可以工作,只是我所有的图像都按日期存储在文件夹中。所以在脚本中它只是一个静态的url。如何将其变成一个函数,以便将其添加到实际的图像页面中,如"www.mysite.com/view-image/123456.png?download“

代码如下:

代码语言:javascript
复制
    <?php
 if (!$filename) {
        // if variable $filename is NULL or false display the message
        echo $err;
    } else {
        // define the path to your download folder plus assign the file name
        $path = 'image.uploads/' .$imagedate/'.$filename;
        // check that file exists and is readable
        if (file_exists($path) && is_readable($path)) {
            // get the file size and send the http headers
            $size = filesize($path);
            header('Content-Type: application/octet-stream');
            header('Content-Length: '.$size);
            header('Content-Disposition: attachment; filename='.$filename);
            header('Content-Transfer-Encoding: binary');
            // open the file in binary read-only mode
            // display the error messages if the file can´t be opened
            $file = @ fopen($path, 'rb');
            if ($file) {
                // stream the file and exit the script when complete
                fpassthru($file);
                exit;
            } else {
                echo $err;
            }
        } else {
            echo $err;
        }
    }
?>
EN

回答 1

Stack Overflow用户

发布于 2013-07-14 11:03:36

如果我没记错的话,您希望在用户导航到路径时下载文件。

您可以使用.htaccess强制下载文件,而不是在浏览器中查看它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17636047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档