网络营销电子商务研究中心  
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:43 AM
Kamila Kamila is offline
初级会员
 
Join Date: 2013-01-19
Posts: 1
Kamila 现在声名狼藉
Default Best way to get files from a dir filtered by certain extension in php

So right now I have a directory and I am getting a list of files
Code:
$dir_f = "whatever/random/";
$files = scandir($dir_f);
That, however, retrieves every file in a directory. How would I retrive only files with a certain extension such as .ini in most efficient way.
Reply With Quote
  #2   IP: 153.99.39.110
Old 2016-08-16, 12:45 AM
Karen Karen is offline
初级会员
 
Join Date: 2014-02-10
Posts: 1
Karen 现在声名狼藉
Default

PHP's glob() function let's you specify a pattern to search for. PHP has a great function to help you capture only the files you need. Its called glob()

glob - Find pathnames matching a pattern
Here is an example usage -
Code:
$files = array();
foreach (glob("/path/to/folder/*.txt") as $file) {
  $files[] = $file;
}
Reply With Quote
  #3   IP: 153.99.39.110
Old 2016-08-16, 12:47 AM
Kasen Kasen is offline
初级会员
 
Join Date: 2009-12-02
Posts: 1
Kasen 现在声名狼藉
Default

Code:
<?php
foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>
or

Code:
//path to directory to scan
$directory = "../file/";

//get all image files with a .txt extension.
$file= glob($directory . "*.txt ");

//print each file name
foreach($file as $filew)
{
echo $filew;
$files[] = $filew; // to create the array

}
Reply With Quote
  #4   IP: 153.99.39.110
Old 2016-08-16, 12:49 AM
Katelyn Katelyn is offline
初级会员
 
Join Date: 2008-02-04
Posts: 2
Katelyn 现在声名狼藉
Default

8
down vote
If you want more than one extension searched, then preg_grep() is an alternative for filtering:
Code:
$files = preg_grep('~\.(jpeg|jpg|png)$~', scandir($dir_f));
Though glob has a similar extra syntax. This mostly makes sense if you have further conditions, add the ~i flag for case-insensitive, or can filter combined lists.
or

glob("{$dir}*.{jpg,*​jpeg,gif,ico,png}", GLOB_BRACE). This would work as well for multiple extensions.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 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 10:56 AM.


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