在php中显示文件夹中的图像 [英] Displaying images from folder in php

查看:85
本文介绍了在php中显示文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有这个代码来显示php中文件夹中的图像:

Hello I have this code to show image from folder in php :

$handle = opendir(dirname(realpath(__FILE__)).'/galerija/accomodation/');
while($file = readdir($handle)) {
    if($file !== '.' && $file !== '..') {
        echo '<img src="galerija/accomodation/'.$file.'" rel="colorbox" />';
    }
}

它正在工作,但我怎么设置按名称或类似的方式显示文件夹分类器,因为我真的需要对图像进行排序,这个脚本只显示随机图像。谢谢。

and it's working everthing is fine but how can I set to show folder sorter by name or something, because I really need to sort that images and this script show's only random images.Thank you.

推荐答案

你应该在这里找到答案:
排序文件通过PHP中的创建/修改日期

You should find your answer here: Sorting files by creation/modification date in PHP

还有其他类似的帖子,你可以获得另一个有用的排序函数。

There are other similar posts where you could get another usefull function for sorting.

这样你的代码应该是这样的:

So that your code should look something like this:

if($h = opendir(dirname(realpath(__FILE__)).'/galerija/accomodation/')) {
  $files = array();
  while(($file = readdir($h) !== FALSE){
    if($file !== '.' && $file !== '..'){
       $files[] = stat($file);
    }
  }

  // do the sort
  usort($files, 'sortByName');

  // do something with the files
  foreach($files as $file) {
            echo '<img src="galerija/accomodation/'.$file.'" rel="colorbox" />';
  }
}

//some functions you can use to sort the files
//sort by change time
//you can change filectime with filemtime and have a similar effect
function sortByChangeTime($file1, $file2){
    return (filectime($file1) < filectime($file2)); 
}

function sortByName{
    return (strcmp($file1,$file2)); 
}

这篇关于在php中显示文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆