2014-12-31 84 views
0

如何显示文档根目录外的图像。准确地说如何在根目录以外的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 
} 
+5

为什么不把文件移动到公共目录?目前,您的应用程序不得不将图像复制到内存中,然后吐出。只要让你的http服务器完成它的内置工作,它会更快。 – castis

+1

@castis我能想到的唯一原因就是如果图像是用户提交的(它似乎是因为文件夹名称是“.../upload/...”),因为它通常是最佳实践不允许用户上传东西到webroot中。 –

+0

@Jonathan Kuhn你是对的,我想做一个安全的上传,如果有任何“黑客”试图破坏脚本,无法访问文件上传。 – Cosmin

回答

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; 
} 

再次我不知道它会工作,因为我不是一个服务器主, 但如果这是你想完成什么我会建议检查一下。

+0

如果我这样做,我可以直接访问这些文件...如果你知道任何为什么只是从localhost(php),而不是直接接受accesibles。 – Cosmin

+1

所以如果是为了安全,所以你必须通过PHP写你的帖子,你可以使用缓存来减少使用 –