图像下载功能


Image download function

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

代码在这里:

    <?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;
        }
    }
?>

如果我答对了你的问题,你希望在用户导航到路径时下载文件。。

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