网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站设计 > PHP
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 153.99.39.110
Old 2016-08-16, 12:53 AM
Kealia Kealia is offline
初级会员
 
Join Date: 2013-01-28
Posts: 1
Kealia 现在声名狼藉
Default How to get only images using scandir in PHP?

Is there any way to get only images with extensions jpeg, png, gif etc while using
Code:
$dir    = '/tmp';
$files1 = scandir($dir);
Reply With Quote
  #2   IP: 153.99.39.110
Old 2016-08-16, 12:54 AM
Kellerton Kellerton is offline
初级会员
 
Join Date: 2011-06-26
Posts: 1
Kellerton 现在声名狼藉
Default

You can use glob
Code:
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos, fnmatch or pathinfo to get the extension is up to you.
Reply With Quote
  #3   IP: 153.99.39.110
Old 2016-08-16, 12:55 AM
Kendall Kendall is offline
初级会员
 
Join Date: 2009-07-07
Posts: 1
Kendall 现在声名狼藉
Default

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;
}
Reply With Quote
  #4   IP: 153.99.39.110
Old 2016-08-16, 12:56 AM
Kennedyville Kennedyville is offline
初级会员
 
Join Date: 2013-05-30
Posts: 1
Kennedyville 现在声名狼藉
Default

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);
I hope this is useful if you want case insensitive and image only extensions.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 09:11 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.