search files names in folder with PHP - php

i have a folder that i will save some files that their name will be the microtime that they will create.
there is any way to load just the group of the files names, that their name is bigger then the microtime that i will write?
(in glob() and in readdir() it load all the files names that in the dir, and just after that you can search whatever you want)
i will have in the folder a lot of files. so it will be inefficient to load all the files names first.
example:
if in the dir i have this files:
1413030375194
1413030375717
1413030377193 // for example, i want it to load from here
1413030377685
1413030378165
1413030378411
i want to load just the files that their name bigger then "1413030377000"
so it will load to me just the files names:
1413030377193
1413030377685
1413030378165
1413030378411

foreach(glob("*") as $file) {
if($file >= $YOUR_MIN_FILE_NAME) {
// do whatever you want...
}
}

$safer = escapeshellarg( $_REQUEST['search'] );
$results = glob( "$dir/*$safer*" );
Replace your file name with $_REQUEST['search']

Related

Move bulk files from one folder to the folders associated with the files

I got a csv file, that looks like the following image :
Now there are pdf files in a dir(resources/pdfFiles) that match the Number columns (1,2,3 etc) of that csv file. Now what I am trying to do is, when a button is triggered, all those pdf files will be moved to another folder. But I have a twist here,The Name column of that csv file are the names of subfolders where the associate numbered files will be stored. For example,pdf files from 1-8 matches with Akaar IT Ltd, therefore these files (1-8) will be moved to the folder called Akaar It Ltd. If there is no such folder exists, then it will be generated automatically.
My code so far are :
$path = resource_path('pdfFiles');
$files = \File::files($path);
foreach($files as $key => $file) {
$filename = basename($files[$key]);
$destinationFilePath = public_path('upload/'.$filename );
rename($files[$key], $destinationFilePath);
}
dd("probably done");
What it's doing is moving all files from one folder to another.

php scandir for given $q search query

i have search values like "search value" (defined as $q) and .txt files called "search value.txt" in /mydir/ directory. How can i scandir /mydir/ for the search value ($q) and put the found .txt file with php include command into page? Or is it a better way to put $q value into php file_get_contents php code (i mean put them together like a txt filename like (q$.txt - searchvalue.txt somehow) and pull the content of the .txt file into page? If yes, how? Thanks in advance.
Suppose you have this directory structure :
mydir
--file1.tx
--file2.tx
index.php
index.php
$filepath = glob("mydir/*.txt");//read all txt files in data directory
//print_r($filepath);//debug purpose
$search = 'file1';
// Browse all files and search the $search string in filenames
foreach ($filepath as $filename) {
// Give us file name only without the extension
$fileNameOnly = basename($filename, '.txt');
// Check if a file name only contains exactly our keyword $search
$isExactMatch = preg_match("/\b".$search."\b/iu", $fileNameOnly);
if ( $isExactMatch ) {
// Include the .txt file as result
require $filename;
}
}
Tested, it does the trick.

PHP: Using glob() to list *.txt and directories?

Instead of listing all the files and directories
foreach(glob("/*") as $file) {
...
}
Is there a way to filter *.txt files and directories? Because with this example
foreach(glob("/*.txt") as $file) {
...
}
Will only get the *.txt files, but not the directories. But to list only directories, this will work:
foreach(glob("/*", GLOB_ONLYDIR) as $file) {
...
}
but will left out the *.txt files. I am trying to avoid using preg_match() or similar operations post reading the directory because it will waste server resources if there are around 5000 unnecessary files (image files with *.jpg, *.png).
Thank you.
Also tried GLOB_BRACE, but it didn't work, so had to use the following code:
$aDir1 = glob('/*.txt'); // current directory
$aDir2 = glob('/*/*.txt'); // subdirectory, one level below
$aDir = asort(array_merge($aDir1, $aDir2));
Thank you.

Auto create directories after uploading with PHP

I'm creating a php site where a company will upload a lot of images. I'd like one folder to contain upto 500-1000 files and PHP automatically creates a new one if previous contains more that 1000 files.
For example, px300 has folder dir1 which stores 500 files, then a new one dir2 will be created.
Are there any existed a solutions?
This task is simple enough not to require an existing solution. You can make use of scandir to count the number of files in a directory, and then mkdir to make a directory.
// Make sure we don't count . and .. as proper directories
if (count(scandir("dir")) - 2 > 1000) {
mkdir("newdir");
}
A common approach is to create one-letter directories based on the file name. This works particularly well if you assign random names to files (and random names are good to avoid name conflicts in user uploads):
/files/a/c/acbd18db4cc2f85cedef654fccc4a4d8
/files/3/7/37b51d194a7513e45b56f6524f2d51f2
In this way:
if ($h = opendir('/dir')) {
$files = 0;
while (false !== ($file = readdir($h))) {
$files++
}
if($files > 1000){
//create dir
mkdir('/newdir')
}
}
You could use the glob function. It will return an array matching your pattern, which you could count for the amount of files.

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