如何显示文档根目录外的图像。准确地说如何在根目录以外的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
}
为什么不把文件移动到公共目录?目前,您的应用程序不得不将图像复制到内存中,然后吐出。只要让你的http服务器完成它的内置工作,它会更快。 – castis
@castis我能想到的唯一原因就是如果图像是用户提交的(它似乎是因为文件夹名称是“.../upload/...”),因为它通常是最佳实践不允许用户上传东西到webroot中。 –
@Jonathan Kuhn你是对的,我想做一个安全的上传,如果有任何“黑客”试图破坏脚本,无法访问文件上传。 – Cosmin