简体   繁体   English

PHP从目录获取单个文件名

[英]PHP get single file name from directory

trying to get a single file from directory (alphabetically) 尝试从目录中获取单个文件(按字母顺序)

and the file types would be png jpeg tiff gif 文件类型为png jpeg tiff gif

final out put would be like echo firstimage.jpg nextiamge.jpg previous.img 最终输出就像回声firstimage.jpg nextiamge.jpg previous.img

cant get it work .. started withi this 无法使它工作..开始于此

this one i recovered from power amendments where not saved (showing work) 我从没有保存的电源修订中恢复了这一功能(显示工作)

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); 

what im trying to make is a slide show ... from directory but i can now output the first file.. 我想做的是从目录播放幻灯片...但是我现在可以输出第一个文件。

but thats it echo "slideshow.php?nextimage.jpg" 但这就是回声“ slideshow.php?nextimage.jpg”

here is basic idea 这是基本思路
$dir = '*.jpg, *.png'; $ dir ='* .jpg,* .png'; etc getfirst name from directory getnext name from directory 等等从目录获取名字firstget从目录获取名字

thanks to Rizier123 $types = array("png", "jpeg", "tiff", "gif"); 感谢Rizier123 $ types = array(“ png”,“ jpeg”,“ tiff”,“ gif”); $files = array(); $ 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];
        }
    }

sorry not fair being voted down i tried to save the work but we have horrible power in my country 对不起,被公平地拒绝,我试图保存工作,但我们在我的国家拥有可怕的权力

This should work for you: 这应该为您工作:

<?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];
        }
    }

?>

possible Output: 可能的输出:

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

this one i recovered from power amendments where not saved (showing work) 我从没有保存的电源修订中恢复了这一功能(显示工作)

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM