如何在根目录外的codeigniter中显示图像

时间:2014-12-31 17:46:38

标签: php codeigniter

如何从文档根目录外显示图像。准确地说

/ <-- root directory
/public_html/ <-- document root 
/application/upload/img <-- here is the source of image

我在codeigniter中制作并且它有效但如果我尝试加载多次会有很大的延迟。我在codeigniter中有一个帮助函数来帮助我并增加我加载文件的时间。

我展示了我写的代码:

这是在控制器中:

public function img($img_name){
    $t = $this->getupload->Get($img_name); // here is taked from DB using MODEL
    header("Pragma: public");
    header("Content-type: {$t[0]['file_type']}"); // Take the file type (eg: images/jpg) from DB
    header('Content-Transfer-Encoding: binary'); 
    flush();
    readfile($t[0]['full_path']); // here take the imgfile from DB
}

1 个答案:

答案 0 :(得分:1)

如果您希望服务器工作,而不是代码。

您应该配置apache或nginx以从该路径获取数据。

在apache htaccess中你可以尝试:

RewriteRule ^uploads/(.*)$ /var/www/application/upload/img/$1 [R=301,NC,L]

我不确定它会起作用,但这是一个想法。

在nginx中,您可以查看以下内容:

location /uploads/* {
    try_files /var/www/application/upload/img/$query_string;
}

我不确定它是否会起作用,因为我不是服务器主人, 但如果这是你想要完成的,我建议你检查一下。