首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像下载-图像无法打开

图像下载-图像无法打开
EN

Stack Overflow用户
提问于 2011-09-29 08:06:59
回答 4查看 2.9K关注 0票数 0

我尝试创建一些代码,这样用户就可以在opencart中下载图像,而不需要右键单击和使用Save as...,但是在我创建了这段代码并下载了图像之后,试图打开图像显示图像是损坏的。

代码语言:javascript
复制
<form action="download.php" method="post" >
<input type="text" name="file" value="<?php echo $image['popup']; ?>" />
<input type="submit" value="download" />
</form>

download.php

代码语言:javascript
复制
<?php
$file= $_POST["file"];

header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header ("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-type: application/pdf');
header('Content-type: image/jpg');
header('Content-type: image/gif');
header('Content-type: image/png');

readfile('$file');

?>
EN

回答 4

Stack Overflow用户

发布于 2011-09-29 08:09:40

您当前的问题是,当在单引号中时,$file将不会被解析,而是字面意思。使用双引号或无引号:

代码语言:javascript
复制
readfile($file);

其他意见:

  • 为什么要发送五种不同的内容类型?那也太没道理了。您应该检测文件的类型并发送适当的图像头(如果您只想提供可下载的资源,则只发送application/octet-stream,而不是尝试在浏览器中显示它)。
  • 您应该检查$file实际上指向要允许下载的文件之一。如果没有这种检查,攻击者就可以以这种方式从您的站点获取任何文件,包括配置文件.

票数 3
EN

Stack Overflow用户

发布于 2011-09-29 08:10:17

您不可能多次定义内容类型。根据文件扩展名确定内容类型,并包括正确的内容类型。

除此之外,请使用这个:

代码语言:javascript
复制
readfile($file);

不需要引用。编辑:使用引号,它甚至不能工作,但它将尝试打开一个名为$file的文件名

票数 0
EN

Stack Overflow用户

发布于 2011-09-29 08:10:29

你发送了多个内容类型的标题?这是错误的。

您必须通过扩展来确定内容类型(.jpg,.pdf,.),然后发送适当的标头。

下面是我最近做的一个下载脚本的片段。

代码语言:javascript
复制
    $fileName   = $_POST['file']; // or whatever
    $file       = 'someDirectory/' . $fileName; // full path of the file, could also be absolute
    $fileLen    = filesize($file);          
    $ext        = strtolower(substr(strrchr($fileName, '.'), 1));

    switch($ext) {
        case 'txt':
            $cType = 'text/plain'; 
        break;              
        case 'pdf':
            $cType = 'application/pdf'; 
        break;
        case 'exe':
            $cType = 'application/octet-stream';
        break;
        case 'zip':
            $cType = 'application/zip';
        break;
        case 'doc':
            $cType = 'application/msword';
        break;
        case 'xls':
            $cType = 'application/vnd.ms-excel';
        break;
        case 'ppt':
            $cType = 'application/vnd.ms-powerpoint';
        break;
        case 'gif':
            $cType = 'image/gif';
        break;
        case 'png':
            $cType = 'image/png';
        break;
        case 'jpeg':
        case 'jpg':
            $cType = 'image/jpg';
        break;
        case 'mp3':
            $cType = 'audio/mpeg';
        break;
        case 'wav':
            $cType = 'audio/x-wav';
        break;
        case 'mpeg':
        case 'mpg':
        case 'mpe':
            $cType = 'video/mpeg';
        break;
        case 'mov':
            $cType = 'video/quicktime';
        break;
        case 'avi':
            $cType = 'video/x-msvideo';
        break;

        //forbidden filetypes
        case 'inc':
        case 'conf':
        case 'sql':                 
        case 'cgi':
        case 'htaccess':
        case 'php':
        case 'php3':
        case 'php4':                        
        case 'php5':
        exit;

        default:
            $cType = 'application/force-download';
        break;
    }

    $headers = array(
        'Pragma'                    => 'public', 
        'Expires'                   => 0, 
        'Cache-Control'             => 'must-revalidate, post-check=0, pre-check=0',
        'Cache-Control'             => 'public',
        'Content-Description'       => 'File Transfer',
        'Content-Type'              => $cType,
        'Content-Disposition'       => 'attachment; filename="'. $fileName .'"',
        'Content-Transfer-Encoding' => 'binary', 
        'Content-Length'            => $fileLen         
    );

    foreach($headers as $header => $data)
        header($header . ': ' . $data); 

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

https://stackoverflow.com/questions/7594255

复制
相关文章

相似问题

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