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
Related
I have a directory with subfolders containing images. I need to display all these on one page, and also their folder name, so something like this:
echo Subfolder name
echo image, image, image
echo Subfolder2 name
echo image2, image2 , image2
etc
I've tried using
$images = glob($directory . "*.jpg");
but the problem is I have to exactly define the subfolder name in $directory, like "path/folder/subfolder/";
Is there any option like some "wildcard" that would check all subfolders and echo foreach subfolder name and its content?
Also, opendir and scandir can't be applied here due to server restrictions I can't control.
Glob normally have a recursive wildcard, written like /**/, but PHP Glob function doesn't support it. So the only way is to write your own function. Here's a simple one that support recursive wildcard:
<?php
function recursiveGlob($pattern)
{
$subPatterns = explode('/**/', $pattern);
// Get sub dirs
$dirs = glob(array_shift($subPatterns) . '/*', GLOB_ONLYDIR);
// Get files in the current dir
$files = glob($pattern);
foreach ($dirs as $dir) {
$subDirList = recursiveGlob($dir . '/**/' . implode('/**/', $subPatterns));
$files = array_merge($files, $subDirList);
}
return $files;
}
Use it like that $files = recursiveGlob("mainDir/**/*.jpg");
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
}
}
I would like to delete all files matching a particular extension in a specified directory and all subtree. I suppose I should be using using unlink but some help would be highly appreciated... Thank you!
you need a combination of this
Recursive File Search (PHP)
And the unlink / delete
You should be able to edit the example instead of echoing the file, to delete it
To delete specific extension files from sub directories, you can use the following function. Example:
<?php
function delete_recursively_($path,$match){
static $deleted = 0,
$dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$deleted_size += filesize($file);
unlink($file);
$deleted++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
delete_recursively_($path.$dir,$match);
}
}
return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>
e.g. To remove all text files you can use it as follows:
<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>
I have a html file listing links to artists' pages. What I want to do is to use a php script to list them instead of listing them manually. I also would like to have a thumbnail image above each corresponding link, but I want to get the links first before I add the images. I'm using the following script but it's not working:
<?php
$directory = "C:/wamp/myprojects/UMVA/web/includes/artists";
$phpfiles = glob($directory . "*.html");
foreach($phpfiles as $phpfile)
{
echo ''.$phpfile.'';
}
?>
The folder containing the html files is artists. It doesn't work using the full pathname and it doesn't work using just 'artists' or '/artists' as the pathname. The 'artists' folder is in the same directory 'web' as the php file with the script. What am I missing here?
this should actually do the trick:
$htmlFiles = glob("$directory/*.{html,htm}", GLOB_BRACE);
source
Not sure where is the error, but you can also use SPL Iterators, like GlobIterator, in a more reusable way. GlobIterator returns SplFileInfo objects that provides many useful informations about your file.
Here are the doc pages:
http://fr2.php.net/manual/en/class.globiterator.php
http://fr2.php.net/manual/en/class.splfileinfo.php
Here is an example:
$it = new GlobIterator('C:/wamp/myprojects/UMVA/web/artists/*.jpg');
foreach ($it as $file) {
// I added htmlspecialchars too, never output unsafe data without escape them
echo '' . htmlspecialchars($file->getFilename()) . '';
}
if your directory is always 'C:/wamp/myprojects/UMVA/web/artists', I think you can try scandir( $dirname ) instead of glob().
Here is a simple script that will look for html files in the current directory create and a hyperlink based on the title tag.
<?php
// Get all HTML files in the directory
$html_files = glob("*.{html,htm}", GLOB_BRACE);
$url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// Print out each file as a link
foreach($html_files as $file) {
$contents = file_get_contents($file);
$start = strpos($contents, '<title>');
if ($start !== false) {
$end = strpos($contents, '</title>', $start);
$line = substr($contents, $start + 7 , $end - $start - 7);
echo "<center>$line</center><br>\n";
}
}
?>
Save this file as index.php place the html files in the folder and browse to the URL.
I have an array filled with paths looking like this:
library/main/single/list.php
library/article/grid/thumbs.php
library/footer/tiny.php
These files and folders exists on my http://localhost/test/
I also have path that is http://localhost/new/
What I want to do
What I need to do is move the files while keeping the current file structure (with directories intact) to the new location.
The result should be like this
http://localhost/new/library/main/single/list.php
http://localhost/new/library/article/grid/thumbs.php
http://localhost/new/library/footer/tiny.php
Is there an easy way to do it or do I have to cut every string by its slash?
You could try:
$from = './test/'; //replace with absolute path if better
$to = './new/';
$paths = array('library/main/single/list.php', 'library/article/grid/thumbs.php', 'library/footer/tiny.php');
$dirs = array();
foreach( $paths as $path ) {
$pathinfo = pathinfo($to.$path);
if (!in_array($pathinfo['dirname'], $dirs) && !file_exists($pathinfo['dirname']) && mkdir($pathinfo['dirname'], 0777, true))
$dirs[] = $pathinfo['dirname'];
copy($from.$path, $to.$path);
}