使用PHP创建.jpg文件的下载链接 [英] Creating a download link for .jpg file using PHP

查看:114
本文介绍了使用PHP创建.jpg文件的下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个应该很简单。我有一个分页的图像库,在每个图像下面是一个小的链接,说下载Comp。这应该允许人们快速下载.jpg文件(使用PHP生成的水印)到他们的计算机。



现在,我知道我可以直接链接到.jpg文件,但这需要用户在新窗口中打开图像,右键单击,另存为...等,而是要下载Comp链接立即启动文件的下载。



PHP.net似乎建议使用readfile(),所以每个下载Comp链接都被回显为?download = true& g = {$ gallery}& i = {$ image}。



然后在页面顶部,我看到$ _GET ['download'] var isset,如果是,我运行以下代码:

  if(isset($ _ GET ['download'])){
$ gallery = $ _GET [ 'G'];
$ image = $ _GET ['i'];
$ file =../watermark.php?src={$gallery}/images/{$image};
header('Content-Description:File Transfer');
header('Content-Type:application / jpeg');
header('Content-Disposition:attachment; filename ='。basename($ file));
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:public');
header('Pragma:public');
header('Content-Length:'。filesize($ file));
ob_clean();
flush();
readfile($ file);

}



链接需要lonnnnnnnnng时间,然后它会打开一个对话框提示,要求您打开或保存文件,但一旦保存并尝试打开它,它表示文件已损坏,无法打开。



任何想法?

解决方案

不要将$文件设置为相对URL。 readfile函数将尝试访问服务器上的php文件。这不是你想要的在你的情况下,看起来像watermark.php文件会发送你想要的内容,所以你可能只需要设置它需要的环境,并包含它。

 <?php 
if(isset($ _ GET ['download'])){
$ gallery = $ _GET ['g'];
$ image = $ _GET ['i'];
$ _GET ['src'] ={$ gallery} / images / {$ image};

header('Content-Description:File Transfer');
header('Content-Type:image / jpeg');
header('Content-Disposition:attachment; filename ='。basename($ image));
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:public');
header('Pragma:public');
ob_clean();
include('../ watermark.php');
退出;
}

另一个(更简单的)方式是修改watermark.php。添加一个查询参数,使其发送适当的标题来强制下载并链接到

 < a href =水印.PHP SRC = filename.jpg&安培;下载=真)> ...< / A> 

watermark.php:

 <?php 
if(isset($ _ GET ['download'])&& $ _GET ['download'] =='true'){
header ('内容描述:文件传输');
header('Content-Type:image / jpeg');
header('Content-Disposition:attachment; filename ='。basename($ src));
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:public');
header('Pragma:public');
}
//继续以文件的其余部分为

此外,您不需要调用flush()。在这一点上不应该有任何输出,所以没有必要。


This one should be easy, I think. I have a paginated image gallery, and under each image is a small link that says "Download Comp". This should allow people to quickly download the .jpg file (with a PHP generated watermark) to their computer.

Now, I know I can just link straight to the .jpg file, but that requires the user to have the image open in a new window, right click, Save As..., etc. Instead, I want the "Download Comp" link to initiate the download of the file immediately.

PHP.net seemed to suggest using readfile(), so each "Download Comp" link is being echoed as "?download=true&g={$gallery}&i={$image}".

Then at the top of the page I catch to see if the $_GET['download'] var isset, and if so, I run the following code:

if(isset($_GET['download'])) {
$gallery = $_GET['g'];
$image = $_GET['i'];
$file = "../watermark.php?src={$gallery}/images/{$image}";
header('Content-Description: File Transfer');
    header('Content-Type: application/jpeg');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: public');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
readfile($file);

}

The link takes a lonnnnnnnnng time, and then it brings up a dialog prompt asking you to Open or Save the file, but once you Save and try to open it, it says the file is corrupt and can't be opened.

Any ideas?

解决方案

Don't set $file to a relative url. The readfile function will try to access the php file on the server. That is not what you want. In your case it looks like the watermark.php file will send the contents you want, so you could possibly just set up the environment it needs and include it.

<?php
if(isset($_GET['download'])) {
    $gallery = $_GET['g'];
    $image = $_GET['i'];
    $_GET['src'] = "{$gallery}/images/{$image}";

    header('Content-Description: File Transfer');
    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename='.basename($image));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: public');
    header('Pragma: public');
    ob_clean();
    include('../watermark.php');
    exit;
}

Another (simpler) way is to modify watermark.php. Add a query parameter to make it send the proper headers to force a download and link to that

<a href="watermark.php?src=filename.jpg&download=true)">...</a>

watermark.php:

<?php
if (isset($_GET['download']) && $_GET['download'] == 'true') {
    header('Content-Description: File Transfer');
    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename='.basename($src));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: public');
    header('Pragma: public');
}
// continue with the rest of the file as-is

Also, you don't need the call to flush(). There should not be any output to send at that point, so it is not necessary.

这篇关于使用PHP创建.jpg文件的下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆