简体   繁体   English

我如何在根目录外部的Codeigniter中显示图像

[英]How can i display a image in codeigniter outside root directory

How can i display a image from outside the document root. 如何从文档根目录外部显示图像。 To be precise 确切地说

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

I made in codeigniter and it works but has a big delay if i try to load multiple times. 我在codeigniter中制成,它可以工作,但是如果我尝试多次加载,则会有很大的延迟。 I this it is in codeigniter a helper function to help me and boost my time to load the file. 我这是在codeigniter中的一个辅助函数,可以帮助我并增加我的文件加载时间。

I show the code what i have write: 我向代码显示我编写的内容:

This is in controller: 这在控制器中:

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
}

if you will want your server to work, and not the code. 如果您想让服务器正常工作,而不是代码。

You should configure your apache or nginx to fetch the data from that path. 您应该将apache或nginx配置为从该路径获取数据。

in apache htaccess you could try: 在Apache htaccess中,您可以尝试:

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

I'm not sure it will work, but this is an idea. 我不确定它是否会起作用,但这是一个主意。

in nginx you can check something like this: 在nginx中,您可以检查如下内容:

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

again I'm not sure it will work, as I'm not a servers master, but if this is what you would like to accomplish I would suggest checking that. 再次,我不确定它是否可以工作,因为我不是服务器主服务器,但是如果您要完成此操作,建议您进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM