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

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;
}

Related

PHP - select ONLY file in folder without foreach()?

I have a script that places a CSV file into a temporary folder where it will remain until another script picks it up for import into my DB. This is a separated script and for various reasons cannot do both the placement into the temp folder AND the consecutive database import.
Since I now have a separated import script, I first need to scan the temp folder and look for the import-file, which is the ONLY file in the folder anyway, but has a constantly changing filename that I cannot pre-define. My question now is, how can I get the filename of said file, assign it to a variable and use this later for the database import?
When using a foreach() loop I end up with an array, but rather would like a string for further usage.
PHP
if(file_exists('./'.$temp)) {
$files = scandir('./'.$temp.'/');
foreach ($files as $attachment) {
if (in_array($attachment, array(".",".."))) continue;
$import_file = $attachment;
}
} else {
die("Temp Folder for $temp could not be found, hence no files exist for import. Operation cancelled.");
}
Since scandir is ordered alphabetically (http://php.net/manual/en/function.scandir.php), you file will always be at the end of the scandir array (on non-unix systems it is the only file, on unix systems it comes after . and ..).
Thus, you just need to get the last item of the array, like so:
$s = scandir("./".$temp."/");
$import_file = $s[count($s)-1];
This code automatically retrieves the name of the third file in the temp folder, so as long as there are no other files in there it should work perfectly.

get a unknown file name in different dir, php

Here is my directory structure,
C:\xampp\htdocs\..
C:\download\20150923abc.xls //abc is a random value
how can I attach the file 20150923abc.xls in php?
Also, how to change the filename after I got it?
Thanks.
Use the glob ability to find references all files of type .xls and then you can use the file name references as you wish. This sidesteps the issue of you not knowing the specific file name.
$files = glob("c:/download/*.xls");
This will produce an array of all .xls files with their full filepath. If you wish to rename or attach these files then you can do this using the glob reference:
rename($files[0], "c:/download/somenewname.xls");
etc. Read more at:
PHP Glob Function
EDIT:
From Comment below:
foreach (glob( $old_folder."*.xls") as $filename)
{
$names = explode('/', $filename);
$just_file_name = end($names);
echo $just_file_name . "----\n";
$new_folder = dirname(FILE)."\\prm\\att\\";
//rename_win($old_folder, $new_folder);
rename($filename, $new_folder.$just_file_name); <== this line changed.
}
unset($filename);
To fix the above code in your comment, you need to change the incorrect variables referenced (there was no array $files[0]) to the ones used in the foreach loop.

How can I delete all files in a folder, which matches a certain pattern?

I have a folder with images.
As example:
z_1.jpg
z_2.jpg
z_3.jpg
//...
I want to delete every image with prefix z_*.jpg. How can I do that?
unlink('z_*.jpg'); ?
You need the exact filename to unlink() a file. So just use glob() to get all files which you want to grab. Loop through the returned array and delete the files, e.g.
<?php
$files = glob("z_*.jpg");
foreach($files as $file)
unlink($file);
?>

How to retrieve all text files from multiple different directories?

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);
?>

Check if a file matches a wildcarded spec, in a given directory, with PHP

I have a directory that files are uploaded to, and I want to be able to display a download link if the file exists. The file however has to match a particular pattern as this is the identifier of who uploaded it.
The pattern starts with /ClientFiles/ then it needs to find all files that starts with the user ID. So for example: /ClientFiles/123-UploadData.xls
So it would need to look in the ClientFiles directory and find all files that start with '123-' no matter what comes after.
Cheers
To look for files by a certain pattern you can use glob, then use is_readable to check if you can read the files.
$files = array();
foreach(glob($dirname . DIRECTORY_SEPARATOR . $clientId . '-*' as $file) {
if(is_readable($file) {
$files[] = $file;
}
}
Simply use the file_exists() function
php has a function file_exists. Use that to make some logic about if you show a link or not.

Categories