【问题标题】:Retrieving image file from relative folder using readfile() or file_get_contents() not working使用 readfile() 或 file_get_contents() 从相关文件夹中检索图像文件不起作用
【发布时间】:2016-10-30 20:07:06
【问题描述】:

我的根文件夹是public_html。在public_html 之外,我有一个名为images 的文件夹。 为了让我从此文件夹中检索图像,我在数据库中查询正确的图像名称和扩展名,该文件存储在 public_html 文件夹 -> forms 文件夹 -> check_images.php

我的数据库输出是:

$output['filetype'] -> jpg
$output['name'] -> 1

变量$file是相对路径+文件名+文件类型,就是'../../images/1.jpg'

现在由于某种原因无法获取图像,但 file_exists() 工作正常。

标题是:

header('Content-type: image/jpeg');

我做错了什么?为什么readfile()file_get_contents()file_exists() 完全相同的文件夹不能正常工作

完整的代码可以在这里找到: http://pastebin.com/MhkAfY4s

当前显示的是空图像/或链接时损坏的图像。

更新

  1. 首先我检查 URL:http://pastebin.com/Fz36GmVD
  2. 然后我运行镜像代码:http://pastebin.com/fZSRuJ4r

【问题讨论】:

  • 你能展示更多你正在尝试使用的代码吗?
  • @Aborted Posted :) 忽略其中的 ' 错误。它在实际应用程序中不存在
  • header('Content-type: image/jpeg); ...它缺少结束单引号。也许您遇到了错误?
  • @Aborted 见上面的评论
  • 如果你 echo 输出文件内容,没有 JPEG 标头,它会显示什么吗?

标签: php file-get-contents readfile file-exists public-html


【解决方案1】:

发生的事情很可能是除了图像之外还有其他输出,从而弄乱了图像二进制数据。查看页面的来源,注意<?php 之前的任何空格以及结尾?>one 之后的任何空格(你应该省略,因为它不是必需的)

为了进一步帮助下载大型文件,我建议您使用 XSendFile 为您处理繁重的工作。

更新:

总而言之,它不起作用的最可能原因是带有gzhandler的ob_start,简单地删除它并没有做任何事情,这可能是因为E-Tag和浏览器仍然显示缓存的内容。

【讨论】:

  • 我已经检查了空格。那里空无一物。每当我删除image header 时,我都会在页面上输出一堆随机字母......而image header 我什么也得不到。
  • 你能粘贴完整的代码,包括 php-tags 并粘贴完整的输出,包括标题。
  • 我添加了 pastebin,请参阅更新。它的完整代码。输出只是来自浏览器的标准损坏图像。没有显示代码。
  • 仍然查看源代码和 pastebin -- 在您刚刚添加到 pastebin 的文件之一中,您使用 ob_start() 和 gz_handler,您是否只是为了测试而尝试删除这些文件?跨度>
  • 我的猜测是电子标签和/或缓存策略迫使您的浏览器显示旧内容。 gzhandler 的 ob_start 可能是原因。
【解决方案2】:
  • 你犯了一些小错误
  • header('Content-type: image/jpeg') 应该放在图片内容之前
  • 检查你的文件夹设置喊它是../../../

所以你是 php 代码:

$file_name = (int)$routes[2];
$file = '../images/'.$file_name;

$sql = "SELECT i.image_filetype as filetype, i.image_type as type, i.image_key "
        . "FROM images i "
        . "WHERE i.image_deleted IS NULL "
        . "AND i.image_id = :filename LIMIT 1";

$results = $db_connect->prepare($sql);
$results->bindParam(':filename', $file_name, PDO::PARAM_INT);
$results->execute();
$db_image = $results->fetchAll(PDO::FETCH_ASSOC);
foreach($db_image as $output){
    // PROFILE PICTURE
    if($output['type'] === '1'){
        $path = $file.'.'.$output['filetype'];
        if(file_exists($path)){
            header('Content-type: image/jpeg');
            echo file_get_contents($path);
            exit;
        }else{$error_code = 4;}
    }
}
die('1');

更新代码

不确定你做错了什么,但下面的代码对我有用。所以请提供结果细节。 我的图像文件 ID godaddy2.jpg 在父 images 文件夹中。

<?php
// ROUTES[2] IS = IMAGE_ID + UNIQID
// EXAMPLE: 23
//$file_name = (int)$routes[2];

$file_name = 'godaddy2';

//$file = '/home/husliste/images/'.$file_name;

$file = '../images/'.$file_name;
//
//$sql = "SELECT i.image_filetype as filetype, i.image_type as type, i.image_key "
//        . "FROM images i "
//        . "WHERE i.image_deleted IS NULL "
//        . "AND i.image_id = :filename LIMIT 1";
//$results = $db_connect->prepare($sql);
//$results->bindParam(':filename', $file_name, PDO::PARAM_INT);
//$results->execute();
//$db_image = $results->fetchAll(PDO::FETCH_ASSOC);
$db_image[] = [
    'filetype' =>'jpg',
    'type' =>'1',
];

function checkPlanAccess($a){
    return true;
}
foreach($db_image as $output){
    if($output['type']){
        // CREATE HEADER
        switch($output['filetype']){
            case "gif": $ctype="image/gif"; break;
            case "png": $ctype="image/png"; break;
            case "jpeg":
            case "jpg": $ctype="image/jpeg";break;
            default:
        }
        header('Content-type: '.$ctype);
        // CREATE FILE PATH
        $file_path = $file.'.'.$output['filetype'];
        // =========================================== //
        // PLAN
        if($output['type'] === '1'){
            if(checkPlanAccess($output['image_key'])){
                if(file_exists($file_path)){
                    echo file_get_contents($file_path);
                }else{$error_code = 4;}
            }else{$error_code = 4;}
        }
        // PROFILE PICTURE
        elseif($output['type'] === '2' && $check_session){
            if(file_exists($file_path)){
                $image = file_get_contents($file_path);
                echo $image;
            }else{$error_code = 4;}
        }
        // COMPANY LOGO
        elseif($output['type'] === '3' && $check_session){
            if(file_exists($file_path)){
                echo file_get_contents($file_path);
            }else{$error_code = 4;}
        }
        // CUSTOMER LOGO
        elseif($output['type'] === '4'){
            if(checkCustomerAccess($output['image_key'])){
                if(file_exists($file_path)){
                    echo file_get_contents($file_path);
                }else{$error_code = 4;}
            }else{$error_code = 4;}
        }
        // CUSTOMER PROJECT IMAGES
        elseif($output['type'] === '5'){
            if(checkCustomerProjectAccess($output['image_key'])){
                if(file_exists($file_path)){
                    echo file_get_contents($file_path);
                }else{$error_code = 4;}
            }else{$error_code = 4;}
        }
        // SUPPLIER LOGO
        elseif($output['type'] === '6'){
            if(checkSupplierAccess($output['image_key'])){
                if(file_exists($file_path)){
                    echo file_get_contents($file_path);
                }else{$error_code = 4;}
            }else{$error_code = 4;}
        }else{
            $error_code = 4;
        }
    }else{
        $error_code = 4;
    }
}
die('1');
?>

【讨论】:

  • 我已经测试了这两个,并且仍然有相同的输出。查看最新测试的更新代码。但是,当我添加错误的文件路径时,我得到了输出 1
猜你喜欢
  • 2017-07-18
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
相关资源
最近更新 更多