如何使图像内联发送,而任何其他文件作为附件发送?

发布于 2024-12-01 16:53:42 字数 927 浏览 1 评论 0原文

我有一个简单的问题。我正在寻找下载文件的功能。我找到了一个,但我有一个小问题。

首先,这是一些功能代码:

header ('Content-type: ' . mime_content_type($file));
header ('Content-Disposition: attachment; filename="'.basename($file).'"');
header ('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT');
header ('Accept-Ranges: bytes');
header ('Cache-control: no-cache, must-revalidate');
header ('Pragma: private');

为了测试功能,我正在使用这个:

$file = 'path/file.rar';
dowload_file($file);

它工作正常并且正在下载文件。但是,如果我使用图像功能,例如:

$file = 'path/image.jpg';
download_file($file);

那么图像也会被下载。但我希望它可供查看而不是下载。

如果我删除这一行:

header ('Content-Disposition: attachment; filename="'.basename($file).'"');

图像会立即显示,但 rar 和 zip 文件将停止下载。

那么如何让图片在不下载的情况下显示,而其他文件却在下载呢?我正在本地主机上进行测试,这可能是问题所在?

I have a simple question. I was looking for function to download file. I have found one but I have small problem.

First, this is some codes of function:

header ('Content-type: ' . mime_content_type($file));
header ('Content-Disposition: attachment; filename="'.basename($file).'"');
header ('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT');
header ('Accept-Ranges: bytes');
header ('Cache-control: no-cache, must-revalidate');
header ('Pragma: private');

To test function I am using this:

$file = 'path/file.rar';
dowload_file($file);

It’s working fine and file is getting downloaded. But if I use the function for image, for example:

$file = 'path/image.jpg';
download_file($file);

Then the image is also getting downloaded. But I want it to be viewed and not downloaded.

If I delete this line:

header ('Content-Disposition: attachment; filename="'.basename($file).'"');

the image is showed immediately but the rar and zip files is stopped being downloaded.

So how to make images being displayed without downloading but any other file being downloaded? I am testing on localhost, may be this is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人练习一个人 2024-12-08 16:53:42

如果您将 Content-Disposition 标头设置为 attachment,则无论文件类型如何,它都会强制下载。

如果您希望以不同的方式处理图像,则必须在 Content-Disposition 标头中包含一个条件,例如:

if (!preg_match('@\.(gif|jpe?g|png)$@', basename($file)) {
    // this is *not* an image, do C-D: attachment header
    header ('Content-Disposition: attachment; filename="'.basename($file).'"');
} // otherwise do nothing

If you have the Content-Disposition header set to attachment, it will force a download regardless of file type.

If you want your images to be handled differently, you'll have to include a conditional on the Content-Disposition header, like:

if (!preg_match('@\.(gif|jpe?g|png)$@', basename($file)) {
    // this is *not* an image, do C-D: attachment header
    header ('Content-Disposition: attachment; filename="'.basename($file).'"');
} // otherwise do nothing
能否归途做我良人 2024-12-08 16:53:42

您必须对可以在浏览器中查看并且应该下载的文件添加条件语句。对于图像和 xml 文件,您可以提供不使用内容处置作为附件的条件。对于其他人来说包括它。

You have to put a conditional statement on the files that can be viewed in browser and that should be downloaded. For images and xml files you can provide condition not to use content dispostion as attachment. For others include it.

空城缀染半城烟沙 2024-12-08 16:53:42

您可以尝试根据文件类型进行切换。如果您采用文件的扩展名:

$ext = pathinfo($file, PATHINFO_EXTENSION);

那么您可以将您的图像列入白名单:

if($ext != 'gif' && $ext != 'jpg' && $ext != 'png' && $ext != 'jpeg')
{
    header ('Content-Disposition: attachment; filename="'.basename($file).'"');
}

或者,将您的下载列入白名单:

if($ext == 'rar')
{
    header ('Content-Disposition: attachment; filename="'.basename($file).'"');
}

You can try switching based on file types. If you take the extension of the file:

$ext = pathinfo($file, PATHINFO_EXTENSION);

Then you can whitelist your images as so:

if($ext != 'gif' && $ext != 'jpg' && $ext != 'png' && $ext != 'jpeg')
{
    header ('Content-Disposition: attachment; filename="'.basename($file).'"');
}

Or, alternatively whitelist your downloads:

if($ext == 'rar')
{
    header ('Content-Disposition: attachment; filename="'.basename($file).'"');
}
偏闹i 2024-12-08 16:53:42

我正在本地主机上进行测试,可能这就是问题所在?

没有。不是本地主机,而是逻辑是你的问题。

如果您想显示图像,请注意显示图像的功能,而不是下载文件。那是不同的任务。

事实上,要显示图像,您必须发送的唯一标头是正确的内容类型标头。

I am testing on localhost, may be this is the problem?

Nope. Not localhost but logic is your problem.

If you want to display an image, you heed a function that displays an image, not downloads a file. That's different tasks.

In fact, to display an image the only header you have to send is proper content-type one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文