Showing posts with label PHP GLOB. Show all posts
Showing posts with label PHP GLOB. Show all posts

Monday, 24 September 2018

Using PHP's glob() function to find files in a directory | Filter files in directory | Regex filter files in directory


The examples below look at a directory with the following, the same example directory as used in the read through directory post
To find all the files in the directory /path/to/directory with a .txt file extension, you can do this:
$files = glob("/path/to/directory/*.txt");
Array
(
    [0] => /path/to/directory/bar.txt
    [1] => /path/to/directory/foo.txt
    [2] => /path/to/directory/link2foo.txt
)
There are flags which can be passed as a second optional parameter. One of these is GLOB_BRACE which means that e.g. {jpg,gif,png} will be expanded to match jpg, gif and png which can be useful if you need to look for a particular set of files by their extension, in this example for image files
$files = glob("/path/to/directory/*.{jpg,gif,png}", GLOB_BRACE);
Array
(
    [0] => /path/to/directory/1.jpg
    [1] => /path/to/directory/2.gif
    [2] => /path/to/directory/3.png
)

Tuesday, 18 September 2018

Using PHP's glob() function to find files in a directory

A couple of weeks ago I posted how to read through a directory with PHP using the opendir() readdir() and closedir() functions and now look at the glob() function. glob() returns the filenames into an array and supports pattern matching so it can be very easy to find e.g. jpg images in a particular directory.

Example directory

The examples below look at a directory with the following, the same example directory as used in the read through directory post:
bar.txt       A regular file
baz           A directory
foo.txt       A regular file
link2foo.txt  A symbolic link to foo.txt

Simple example

To find all the files in the directory /path/to/directory with a .txt file extension, you can do this:
$files = glob("/path/to/directory/*.txt");
The $files array contains the following from the example directory:
Array
(
    [0] => /path/to/directory/bar.txt
    [1] => /path/to/directory/foo.txt
    [2] => /path/to/directory/link2foo.txt
)
If no files matched the pattern then the array will be empty.

Example using braces

There are flags which can be passed as a second optional parameter. One of these is GLOB_BRACE which means that e.g. {jpg,gif,png} will be expanded to match jpg, gif and png which can be useful if you need to look for a particular set of files by their extension, in this example for image files.
If the example directory also had the files 1.jpg, 2.gif and 3.png then you can do this to get glob to return just the image files:
$files = glob("/path/to/directory/*.{jpg,gif,png}", GLOB_BRACE);
print_r($files) would echo:
Array
(
    [0] => /path/to/directory/1.jpg
    [1] => /path/to/directory/2.gif
    [2] => /path/to/directory/3.png
)

Further reading

This serves as an introduction to using glob() to find files with PHP. Read the glob manual page for more details and for information about the other flags.

Related posts:

Thursday, 30 August 2018

delete a new line from the txt file after inserting the value

I create a txt file from a folder in that way:

$fp = fopen("mylist.txt","rw+");
foreach(glob("folder/*.*") as $value){
fwrite($fp,$value."\n");
}

and it create inside the txt each items, one per line, BUT it also add a newline at the end of the file.
This is the content of mylist.txt:
foldelr/file1.mp3
folder/file2.mp3
(BLANK NEWLINE)

I tried to remove the blank newline at the end of the txt file, in this way:
$filetxt = fopen("mylist.txt","r");
$rtrim = rtrim($filetxt, "\n");
$newfile = file_put_contents("newfile.txt", $rtrim);

But it doesn't work, because in the "newfile.txt", i still have the newline blank after last file name.
I tried to convert file in array, use "array_diff()" to remove the last line, but it fail. I tried also all the suggestions in this thread: remove new line characters from txt file using php but not fix my problem.
Does anyone can please help me to understand what i'm missing or mistaken?
Kind Regards
Brus

Use file_get_contents method it works fine for me
    $fp = fopen("mylist.txt","rw+");
    foreach (glob("test/*.*") as $value) {
        fwrite($fp, $value . "\n");
    }

    $filetxt = file_get_contents("mylist.txt");
    $rtrim =  rtrim($filetxt, "\n");
    $newfile = file_put_contents("mylist.txt", $rtrim);

Or You can also remove the last newline by using the array count.
$fp = fopen("mylist.txt","rw+");
$i =0;
foreach(glob("folder/*.*") as $value){
  $i++;
  if($i < count(glob("folder/*.*")))
    fwrite($fp,$value."\n");
  else
    fwrite($fp,$value);
}

Saturday, 4 October 2014

glob in PHP

glob — Find pathnames matching a pattern
Syntax:

array glob ( string $pattern [, int $flags = 0 ] )
The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

Parameters:

pattern
The pattern. No tilde expansion or parameter substitution is done.

flags
Valid flags:

GLOB_MARK - Adds a slash to each directory returned
GLOB_NOSORT - Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically
GLOB_NOCHECK - Return the search pattern if no files matching it were found
GLOB_NOESCAPE - Backslashes do not quote metacharacters
GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
GLOB_ONLYDIR - Return only directory entries which match the pattern
GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.
Return values: Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.

Note:
On some systems it is impossible to distinguish between empty match and an error.
Changelog ¶

Version Description
5.1.0 GLOB_ERR was added


Example #1 Convenient way how glob() can replace opendir() and friends.

<?php
foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>
The above example will output something similar to:

funclist.txt size 44686
funcsummary.txt size 267625
quickref.txt size 137820


Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.
Note: This function isn't available on some systems (e.g. old Sun OS).
Note: The GLOB_BRACE flag is not available on some non GNU systems, like Solaris.