How to retrieve all text files from multiple different directories? - php

You may have heard of the glob method but that only manages to retrieve files from the directory that the file containing the method is located on - only one directory.
This is an example of the code that I am using:
<?php
foreach (glob("*.txt") as $filename)
{
$time = filemtime($filename);
$files[$time] = $filename;
}
krsort($files);
foreach ($files as $file) {
echo $file;
}
?>
What happens here is that all of the text files in the current directory are retrieved, they are then sorted by order of date modified and are then echoed out onto the page.
The problem with this is that I don't just want to retrieve text files from the one directory.
How would I change this so that I can retrieve files from multiple directories of my choice all from one page - so I can echo out all of the text files from the multiple directories onto one page rather than only echoing out the text files from one directory?
I believe I would need to store all the directories I want to glob into an array but I am not sure how to retrieve it.

Here is an example stolen from the page in my comment above
<?php
$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.txt$/i', RecursiveRegexIterator::GET_MATCH);
?>

Related

PhP get content of all files in directory

I have many files in a directory that look similar to:
personalchat.spud.3353.1789
personalchat.guest.3355.1789
personalchat.ken.3355.1789
each file essentially has a few lines of html inside, and wanting to open all files, pull the content to the screen. I can do it by hardcoding a single filename but not all at once or by using a * for filename
Code:
$names=file('/var/www/html/web/www/chat.support/status/personalchat*');
foreach($names as $name)
{
echo $name.'<br>';
}
##UPATE 2
I tried changing to an array but it only displays the name of the files not the content of each:
I am able to list the files as an array:
personalchat.mick.3350.1733
personalchat.guest.3352.1739
personalchat.test.3351.1736
But not able to display the name of each file but not the content:
$mydir = '/var/www/html/web/www/chat.support/status/';
$myfiles = array_diff(scandir($mydir), array('.', '..'));
foreach($myfiles as $name)
{
echo $name.'<br>';
}
Thanks
file() function reads 1 file. So you need an array of files to read with file().
May be you can try to create such array using scandir() first, and then read the file 1 by one.

PHP Script to Sort a Large Folder of Files into Date based Sub Folders

I have a large list of call recordings from a PBX system and I need to sort them into more manageable folders.
Being a bit of a linux noob, how would I search through a specified folder and based on the date of the file, move it into a folder hierarchy.
Example I have 2013/04/23 as the folder structure.
Then I have files with dates as
20130403-blah283.mp3
20130403-blah284.mp3
20130403-blah285.mp3
20130403-blah286.mp3
20130403-blah287.mp3
etc. Any help would be appreciated.
This might give you a starting point:
get files
iterate through files
create new directory/file_name
create directory if !exists
copy file
delete old file
Model:
<?php
$base_path = "/PATH_TO_FILES/";
//$files = glob($base_path."*.mp3");
$files = array('20130403-blah283.mp3','20130403-blah284.mp3','20130403-blah285.mp3','20130403-blah286.mp3','20130403-blah287.mp3'); // for testing
foreach($files as $file){
$pieces = explode("-",$file);
$new_filename = $pieces[1];
$timestamp = $pieces[0];
$directory = substr($timestamp,0,4)."/".substr($timestamp,4,2)."/".substr($timestamp,6)."/";
if(!is_dir($base_path.$directory)){
mkdir($directory,0777);
}
copy($base_path.$file,$base_path.$directory.$new_filename);
//unlink($base_path.$directory.$new_filename); // DELETES OLD FILE ONLY UNCOMMENT WHEN YOUR READY!
}

Search for file in folder using php

I am storing user uploaded files like pdf images and txt files in separate folders using my php script i want to retrieve the file names from the folder upload and give the pdf and txt in a group and also way to search for specific file.
I also need to rename the file before to $ja variable
$ja
$da = date("dmY");
$ja = $uid.$da;
move_uploaded_file($mi, $uploadpath)
also used this code which i found in stack
Example 01:
<?php
// read all files inside the given directory
// limited to a specific file extension
$files = glob("./ABC/*.txt");
?>
Example 02:
<?php
// perform actions for each file found
foreach (glob("./ABC/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
the scandir(); function will help you
<?php
$dir = '/tmp';
$files1 = scandir($dir);
print_r($files1);
?>
http://www.php.net/manual/en/function.scandir.php
now you have array of all files in a location you specified you can use array functions to get your work done
you can try listing the directory with scandir, and then filter as you want in the php array of filenames you will get

PHP glob() breaks on WordPress sub pages

I've been struggling with this for a while now.
I've got an image gallery running using jQuery cycle plugin and the files are pulled from a folder using PHP glob(). Problem is, when I navigate to another page the gallery breaks due to the url of the new page being tacked on at the beginning of the file path.
Example:
Front Page url: http://localhost/project/image-display-images/image.jpg
Other Page url: http://localhost/**NEWPAGE**/project/image-display-images/image.jpg
Here's my code:
$files = glob('image-display-images/*.*');
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'"'.' alt="Campus Images" width="362" height="246"/>';
}
This would generate a list of images for jQuery cycle to scroll through. It only works on the front page though.
Any ideas?
SOLVED!
Here is my new code:
$files = glob(ABSPATH.'/image-display-images/*.*');
foreach ($files as $f) {
echo '<image src="'.home_url(str_replace(ABSPATH,'',$f)).'"alt="Campus Images" width="362" height="246"/>';
}
This works on all pages.
Thank you!
Use an absolute path:
$files = glob(ABSPATH.'image-display-images/*.*');
The WordPress Core sets the ABSPATH constant so it should be fairly reliable.
glob deals with filesystem paths, but you are trying to load URLs. To display the files the way you are trying to, you will need to convert the results to URLs. Here is a bare-bones example.
$files = glob(ABSPATH."*.*");
foreach ($files as $f) {
echo home_url(str_replace(ABSPATH,'',$f));
}
You may better off writing you own function to grab your file names, rather than depending on glob which does come with a warning about not being available on some systems. See: http://codex.wordpress.org/Filesystem_API
Define the full path of your gallery instead of 'image-display-images/*.*'
For example glob('/var/etc/www/image-display-images/*.*')

Select file(s) in a directory based upon complex filename

I have audio files in var/
This is the file name
2-3109999999-3246758493-1271129518-1271129505.6.wav
Format
2=campaign id
3109999999=caller id
3246758493=number called
1271129518=timestamp call ended
1271129505=timestamp call started
6=call id
If I were to pass just the number called which was 3246758493, how can I find all the files without defining all the other variables(such as timestamp, etc) and just the files that have that number in the filename?
You would need to loop though the folder: http://php.net/manual/en/function.readdir.php
Then for each of the files in the folder, try and match it to the file that was requested using regex I guess?
http://www.txt2re.com/index-php.php3?s=2-3109999999-3246758493-1271129518-1271129505.6.wav&8
You could also use a DirectoryIterator to scan the folder and a RegexIterator to filter the files based on a pattern.
$id = '3246758493';
$files = new RegexIterator(new DirectoryIterator('var/'),
"#^\d-\d{10}-$id-\d{10}-\d{10}\.\d\.wav$#D");
foreach ($files as $fileinfo) {
echo $fileinfo . PHP_EOL;
}

Categories