我正在尝试上传目录(/images/users/photos)中的图像文件并在 View 中显示上传的图像。这是工作。 这是我在 Controller 中上传图像文件的代码。
public function actionImage() {
$model = new ImageUpload ( 'user' );
if (isset ( $_POST ['ImageUpload'] )) {
$model->attributes = $_POST ['ImageUpload'];
$model->image = CUploadedFile::getInstance ( $model, 'image' );
if ($model->validate ()) {
$path = realpath ( dirname ( $model->image ) );
$imagePath = $path . Yii::app ()->params ['logoUploadPath'] . $model->image;
$model->image->saveAs ( $imagePath );
}
} else {
Yii::app ()->user->setFlash ( 'image-error', "Please select file!" );
}
$this->redirect ( 'index', array (
'model' => $model
) );
}
在 View 中,我使用以下代码显示上传的图像。
<?php
$files = CFileHelper::findFiles(Yii::app()->basePath."/..".Yii::app()->params['logoUploadPath'],array('fileTypes'=>array('gif','png','jpg','jpeg'),'level'=>0));
foreach ($files as $file)
{
$upload = new ImageUpload();
$image = basename($file);
$src = Yii::app()->params['logoUploadPath'].$image;
?>
<div class="img-wraplogo">
<?php echo CHtml::image($src,$image,array("title"=>'Image', 'class'=>'existing-thumbnail-image')); ?>
</div>
<?php }}?>
问题是 图像文件在 View 中按字母顺序显示。但我想按上传顺序显示它们。
例如:我上传了两个文件,先是 B.png,然后是 A.png,我的 View 先显示 A.png,然后显示 B.png。但我想先显示 B.png 然后显示 A.png。
请帮我弄清楚上述场景或任何实现此目的的提示。 我上传的图片路径是/images/users/photos
最佳答案
正如您从 CFileHelper
中看到的那样方法:
public static function findFiles($dir,$options=array())
{
$fileTypes=array();
$exclude=array();
$level=-1;
extract($options);
$list=self::findFilesRecursive($dir,'',$fileTypes,$exclude,$level);
sort($list);
return $list;
}
使用sort()
函数。您不需要这种默认排序方法。但是,此方法不接受参数来修改排序功能。
因此,唯一的方法是编写您自己的从 CFileHelper
扩展的 CustomFileHelper
类,并重写您自己的按日期排序的函数。
如果您实际上编写了一些代码来扩展此类,但遇到问题,请使用您尝试过的代码发布一个新问题。
或者,您可以搜索 non-Yii PHP solution .
关于php - 按上传顺序检索上传的文件 yii php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25758927/