![]() |
|
|||||||
![]() |
|
|
Thread Tools | Display Modes |
|
#1
IP: 153.99.39.110
|
|||
|
|||
|
So right now I have a directory and I am getting a list of files
Code:
$dir_f = "whatever/random/"; $files = scandir($dir_f); |
|
#2
IP: 153.99.39.110
|
|||
|
|||
|
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;
}
|
|
#3
IP: 153.99.39.110
|
|||
|
|||
|
Code:
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
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
}
|
|
#4
IP: 153.99.39.110
|
|||
|
|||
|
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));
or glob("{$dir}*.{jpg,*jpeg,gif,ico,png}", GLOB_BRACE). This would work as well for multiple extensions. |
![]() |
| 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 |