Php 使用Yii提供web应用程序文件夹之外的图像,性能问题
我有以下控制器/操作:Php 使用Yii提供web应用程序文件夹之外的图像,性能问题,php,image,performance,yii,Php,Image,Performance,Yii,我有以下控制器/操作: <?php class ImageController extends CController { public function actionView($folder, $name) { $ds = DIRECTORY_SEPARATOR; $file = Yii::app()->params['images_path'] . $ds . $folder . $ds . $name; $con
<?php
class ImageController extends CController {
public function actionView($folder, $name) {
$ds = DIRECTORY_SEPARATOR;
$file = Yii::app()->params['images_path'] . $ds . $folder . $ds . $name;
$content = file_get_contents($file);
Yii::app()->request->sendFile($file, $content);
}
}
实际上sendFile()
用于提供下载服务,而不是查看文件内容。
老实说,我不明白你想做什么,但从我的实践来看,带有sendFile()
的文件可能会损坏。有不同的原因,其中一个是日志记录,这也会导致一些性能问题。
移除除CFileLogRoute之外的所有记录器。
可以在配置或基本控制器中完成:
public function disableProfilers()
{
if (Yii::app()->getComponent('log')) {
foreach (Yii::app()->getComponent('log')->routes as $route) {
if (in_array(get_class($route), array('CProfileLogRoute', 'CWebLogRoute', 'YiiDebugToolbarRoute','DbProfileLogRoute'))) {
$route->enabled = false;
}
}
}
}
然后您的下载操作将被删除
public function actionDownloadFile()
{
$this->disableProfilers();
$file = '/path/to/file/some_file.txt';
Yii::app()->request->sendFile(basename($file),file_get_contents($file));
}
使用php和file_get_内容提供文件是一种高效的操作,因此,如果您有很多图像要提供,请通过HTTP服务器提供它们。这将是一项昂贵的操作;您正在读取图像文件的全部内容,将其分配给一个PHP变量,并通过框架为其提供服务。理想情况下,您希望通过HTTP服务器直接为静态文件(如图像)提供服务。