PHP标头('Content-Type: image/jpeg')在本地主机上工作,但在真实服务器上不起


PHP header('Content-Type : image/jpeg') works on localhost but not working on real server

我想从用户隐藏图像url,所以我使用php header()函数,当我在Bluehost上托管我的网站时,问题出现了,它在localhost上工作得很好。下面是我访问图像的函数。

function download($name = '', $tiny = '0') {
    if ($name != '') {
        $file = $this->mdl->get_file($name);
        if($file){
                $mime = mime_content_type('./'.$file->path.$file->name);
                header('Content-Type: '.$mime);
                echo file_get_contents('./'.$file->path.$file->name);
        }
    }
}

这里是模型上的get_file函数:

function get_file($name=''){
    if($name!=''){
        return $this->db->select('*')
        ->from('docs')
        ->where('name',$name)
        ->get()
        ->row();
    }
    return false;
}

基本上这种类型的问题发生在php标记之外的任何空格或新行,或者在header函数之前打印的任何内容。

  • Try this:

ob_clean (); 之前头

标头只能在任何数据发送到浏览器之前设置。

检查<?php标记前是否有空格

检查文件是否保存为没有BOM的UTF-8格式,bo标记可以被解释为输出。

检查主机是否为您的页面添加了额外的内容/标题