简体   繁体   中英

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. I this it is in codeigniter a helper function to help me and boost my time to load the file.

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.

in apache htaccess you could try:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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