![]() |
|
|||||||
![]() |
|
|
Thread Tools | Display Modes |
|
#1
IP: 153.99.39.110
|
|||
|
|||
|
Is there any way to get only images with extensions jpeg, png, gif etc while using
Code:
$dir = '/tmp'; $files1 = scandir($dir); |
|
#2
IP: 153.99.39.110
|
|||
|
|||
|
You can use glob
Code:
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
|
|
#3
IP: 153.99.39.110
|
|||
|
|||
|
I would loop through the files and look at their extensions:
Code:
$dir = '/tmp';
$dh = opendir($dir);
while (false !== ($fileName = readdir($dh))) {
$ext = substr($fileName, strrpos($fileName, '.') + 1);
if(in_array($ext, array("jpg","jpeg","png","gif")))
$files1[] = $fileName;
}
|
|
#4
IP: 153.99.39.110
|
|||
|
|||
|
Here is a simple way to get only images. Works with PHP >= 5.2 version. The collection of extensions are in lowercase, so making the file extension in loop to lowercase make it case insensitive.
Code:
// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
// init result
$result = array();
// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');
// iterate
foreach ($directory as $fileinfo) {
// must be a file
if ($fileinfo->isFile()) {
// file extension
$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
// check if extension match
if (in_array($extension, $extensions)) {
// add to result
$result[] = $fileinfo->getFilename();
}
}
}
// print result
print_r($result);
|
![]() |
| Currently Active Users Viewing This Thread: 2 (0 members and 2 guests) | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| X-Cart Multiple Input Validation Holes Permit SQL Injection and Cross-Site Scripting | topvip | X-Cart | 0 | 2009-07-21 10:03 AM |
| 用php语言来编写shell脚本 | car | 代码交流 | 0 | 2008-05-05 08:09 PM |
| Php教程.经验技巧(上) | sunshine | 代码交流 | 0 | 2006-12-15 08:13 PM |
| Php入门速成 | smiling | 代码交流 | 0 | 2006-12-15 07:30 PM |
| php.ini中文解释 | sunshine | 服务器环境搭建 | 0 | 2006-02-04 11:05 PM |