开发者

check for files with PHP [duplicate]

This question already has answers here开发者_运维百科: Closed 10 years ago.

Possible Duplicate:

Get the Files inside a directory

Is there a function that can be used to get the contents of a directory (a photo gallery directory for example) ?

I'm trying to save time on a project by automating a photo gallery based on which files are available.

Thanks

Shane


You can either use the DirectoryIterator:

$dir = new DirectoryIterator('path/to/images');
foreach ($dir as $fileinfo) {
    echo $fileinfo->getFilename() . "\n";
}

or alternatively glob():

$filenames = glob('path/to/images/*.jpg');
foreach ($filenames as $filename) {
    echo $filename ."\n";
}


glob()

scandir()


I use a while loop to grab a list of files, omit the 2nd if statement if you want to grab a all files.

if ($handle = opendir('/photos/')) {
  while(false !== ($sFile = readdir($handle))) {
     if (strrpos($sFile, ".jpg") === strlen($sFile)-strlen(".jpg")) {
        $fileList[] = $sfile;
     }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜