PHP从目录中获取单个文件名

时间:2015-01-08 17:27:04

标签: php image glob

尝试从目录中获取单个文件(按字母顺序排列)

,文件类型为png jpeg tiff gif

最终出局就像 echo firstimage.jpg nextiamge.jpg previous.img

无法让它工作.. 从这个开始

这一个我从没有保存的电力修正案中恢复过来(显示工作)

function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 ); 
$num = array_rand($ar); 
return $ar[$num];
}

function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file; 
}
}
closedir($img_dir); 
}
return $images; 
echo $images;
}

$root = '/ers/m'; 
// If images not in sub directory of current directory specify root 
//$root = $_SERVER['DOCUMENT_ROOT'];

$path = ''; 

// Obtain list of images from directory 
$imgList = getImagesFromDir($root . $path);

$img = getRandomFromArray($imgList); 

我想要制作的是幻灯片......来自目录 但我现在可以输出第一个文件..

但就是这样     echo“slideshow.php?nextimage.jpg”

这是基本思路 $ dir ='* .jpg,* .png';等等 目录中的getfirst名称 目录中的getnext名称

感谢Rizier123         $ types = array(“png”,“jpeg”,“tiff”,“gif”);         $ files = array();

    foreach($types as $type) {
        $files[$type] = glob("*.$type");
    }

    array_multisort($files);

    foreach($files as $key => $type) {
        if(count($type) > 0) {
            echo "Type: " . $key . " First file: " . $type[0];
        }
    }
很抱歉不公平被投票我试图挽救工作,但我们在我的国家拥有可怕的权力

2 个答案:

答案 0 :(得分:2)

这应该适合你:

<?php

    $types = array("png", "jpeg", "tiff", "gif");
    $files = array();

    foreach($types as $type) {
        $files[$type] = glob("*.$type");
    }

    array_multisort($files);

    foreach($files as $key => $type) {
        if(count($type) > 0) {
            echo "Type: " . $key . " First file: " . $type[0];
        }
    }

?>

可能的输出:

Type: png First file: gre.png
Type: gif First file: 1.gif

答案 1 :(得分:0)

这一个我从没有保存的电力修正案中恢复过来(显示工作)

function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 ); 
$num = array_rand($ar); 
return $ar[$num];
}

function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file; 
}
}
closedir($img_dir); 
}
return $images; 
echo $images;
}

$root = '/ers/m'; 
// If images not in sub directory of current directory specify root 
//$root = $_SERVER['DOCUMENT_ROOT'];

$path = ''; 

// Obtain list of images from directory 
$imgList = getImagesFromDir($root . $path);

$img = getRandomFromArray($imgList);