duanfeng7756 2017-01-13 10:01
浏览 46

PHP从webroot外部提供图像

Today I was working on my website's function.php in an area that had nothing to do with serving images. I have a php script which gets images from outside of the webroot and serves them based on $_GET for the moment. One minute it was working and then the next it was not. I have no idea what happened. I chalked it up to MAC Sierra's BS server management but moving everything to a LINUX server made no difference.

    <?php
    include('includes/functions.php');  
    $path = $privatePath;
    $uid = getUserProfileImage();
    $img = $_GET['img'];    
    $file = $path."/img/users/".$uid."/".$img; 
    $fallback = "img/site/login.png"; 

    //DETERMINE TYPE 
    $var = explode ('.', $img);
    $ext = array_pop($var); 
    $allowed['gif'] = 'image/gif'; 
    $allowed['png'] = 'image/png'; 
    $allowed['jpg'] = 'image/jpeg'; 
    $allowed['jpeg'] = 'image/jpeg'; 

    if(file_exists($file) && $ext != '' && isset($allowed[strToLower($ext)])) { 
        $type = $allowed[strToLower($ext)]; 
    } else { 
        $file = $fallback; 
        $type = 'image/png'; 
    } 

    header('Content-Type: '.$type);
    //header ("Content-Disposition: inline; filename=\"{$img}\");
    header("Content-length: ".(string)(filesize($file)));  
    //echo file_get_contents($file);
    readfile($file); 
    exit(); 

?>

This code was working just fine a from one min to the next. I have checked the permissions of the folder and verify that it is owned by www-data. Firefox reveals that the image has an error. Usually I had that error message when I was echoing something before the output buffer. But now it is for who knows what reason.

The RAW Data output in Firefox is as such when reading the image:

CgoKCv/Y/+EeQUV4aWYAAElJKgAIAAAADAAAAQMAAQAAAAACAAABAQMAAQAAAAACAAACAQMAAwAAAJ4AAAAGAQMAAQAAAAIAAAASAQMAAQAAAAEAAAAVAQMAAQAAAAMAAAAaAQUAAQAAAKQAAAAbAQUAAQAAAKwAAAAoAQMAAQAAAAIAAAAxAQIAHgA ...

And that goes on for a bit. I have resaved the image and about everything else that I can think of. I have even deleted all work done from prior when the script worked and no change. Anyone got any ideas? Or is there more error that I can log to see the problem and get a hint?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?