How to get directory names in php - php

how can I get all folders in a directory and make
if name of folder == $variable {
code...
}
Can you help-me please?

use glob package
elementsIncurrendDir = glob("regExpFiler*.[tT][xX][tT]")
for s in elementsIncurrendDir:
if s== 'whatever':
pass
If you want to verify that it is a folder you can use:
if os.path.isdir(s):

You can get files and folders by using scandir() function which returns array of filenames.
Function array_diff() computes difference between two arrays and effectively removes filenames . and .. from the array of filenames.
$directory = '/path/to/my/directory';
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
You can then check if file is a directory using is_dir() function and also check for name of the folder.
foreach ($scanned_directory as $filename) {
if (is_dir($filename) && $filename == "folder_name") {
// filename is directory with name folder_name
}
}

Related

how to check for certain file extension in a folder with php [duplicate]

This question already has answers here:
Getting the names of all files in a directory with PHP
(15 answers)
Closed 4 years ago.
I have a folder named uploads with a lot of files in it. I want to find out if there is a .zip file inside it. How can i check if there is a .zip file inside it with php?
Use the glob() function.
$result = glob("my/folder/uploads/*.zip");
It will return an array with the *.zip-files.
Answer is already given by #Bemhard, i am adding more information for future use:
If you want to run script inside your uploads folder than you just need to call glob('*.zip').
<?php
foreach(glob('*.zip') as $file){
echo $file."<br/>";
}
?>
If you have multiple folders and containing multiple zip files inside the folders, than you just need to run script from root.
<?php
$dir = __DIR__; // will get the exact path
$dirs = array_filter(glob('*'), 'is_dir'); // filter is directory or not.
$i = 1;
foreach ($dirs as $key => $value) {
foreach(glob($value.'/*.zip') as $file){
echo $file."<br/>"; // this will print all files inside the folders.
}
$i++;
}
?>
One Extra point, if you want to remove all zip files with this activity, than you just need to unlink file by:
<?php
$dir = __DIR__; // will get the exact path
$dirs = array_filter(glob('*'), 'is_dir'); // filter is directory or not.
$i = 1;
foreach ($dirs as $key => $value) {
foreach(glob($value.'/*.zip') as $file){
echo $file."<br/>"; // this will print all files inside the folders.
unlink($file); // this will remove all files.
}
$i++;
}
?>
References:
Unlink
Glob
This also could help, using scandir and pathinfo
/**
*
* #param string $directoryPath the directory to scan
* #param string $extension the extintion e.g zip
* #return []
*/
function getFilesByExtension($directoryPath, $extension)
{
$filesRet = [];
$files = scandir($directoryPath);
if(!$files) return $filesRet;
foreach ($files as $file) {
if(pathinfo($file)['extension'] === $extension)
$filesRet[]= $file;
}
return $filesRet;
}
it can be used like
var_dump(getFilesByExtension("uploads/","zip"));

PHP Find files with specific name part in folder

Is there a way to get all the files with a specific name from a folder with php?
For example I have in folder next files:
6546-da6sd.png
465-dasd.jpg
654-548484.jpg
654-sasaf.png
654-sakj879.jpg
776-54fsdfs.png
....
I want to get all files that start with "654-" (but not the first (6546-da6sd.png))
Thank You!
you can use glob() and strpos() like:
<?php
$dir = 'files/';
$key = '654-';//search key
foreach (glob("$dir*") as $file) {
$file = str_replace($dir,'',$file);
if (strpos($file, $key) == 0) {
echo($file."<br>");
}
}
?>

php glob() does not show files from another domain?

I am trying to read all files from a directory and store it in an array.But the file which contains this code is on www.xyz.com domain and i want to check files exist on https://www.test.com/prod_images/.But glob is returning empty array.Why is it returning empty array ?
foreach (glob('https://www.test.com/prod_images/*', GLOB_ONLYDIR) as $dir) {
$dirname = basename($dir);
$items[] = $dirname;
}
print_r($items);

PHP Get file name starting with prefix

This is a custom function. At the moment, this function get all the file in the default directory, strip ".php" and list them.
The problem is that I want to only get files from the directory which has starting prefix "tpl-" Example : tpl-login-page.php
/* Get template name of the file */
function get_template_name (){
$files = preg_grep('~\.(php)$~', scandir(admin . "templates/default/"));
foreach($files as $file){
$file = str_replace('.php','',$file);
echo $file . "<br/>";
}
}
You need to change the regular expression in preg_grep:
$files = preg_grep('~^tpl-.*\.php$~', scandir(admin . "templates/default/"));
Explanation:
^tpl- - starting with "tpl-"
.* - any characters
\.php$ - ending with ".php"
I like another, simple way:
1. get all files in folder
$path = './images';
$files = glob($path.'/*');
2. get all files having extension .jpg
$path = './images';
$files = glob($path.'/*.jpg');
3. get all files having prefix myprefix_
$path = './images';
$files = glob($path.'/myprefix_*');
$target_file_png = glob($target_dir.'/group_'.$groupId.'*.png');
$target_file_png will return an array containing all the files in folder specified in the path $target_dir starting with '/group_'.$groupId.' and specify the file format as *.png

I need to find a file in directory and copy it to a different directory

I merely have the file name, without extension (.txt, .eps, etc.)
The directory has several subfolders. So, the file could be anywhere.
How can I seek the filename, without the extension, and copy it to different directory?
http://www.pgregg.com/projects/php/preg_find/preg_find.php.txt seems to be exactly what you need, to find the file. then just use the normal php copy() command http://php.net/manual/en/function.copy.php to copy it.
https://stackoverflow.com/search?q=php+recursive+file
have a look at this http://php.net/manual/en/function.copy.php
as for seeking filenames, could use a database to log where the files are? and use that log to find your files
I found that scandir() is the fastest method for such operations:
function findRecursive($folder, $file) {
foreach (scandir($folder) as $filename) {
$path = $folder . '/' . $filename;
# $filename starts with desired string
if (strpos($filename, $file) === 0) {
return $path;
}
# search sub-directories
if (is_dir($path)) {
$result = findRecursive($path);
if ($result !== NULL) {
return $result;
}
}
}
}
For copying the file, you can use copy():
copy(findRecursive($folder, $partOfFilename), $targetFile);

Categories