I have a complex folder structure, with folders inside folders etc.. and I want to display the structure in a html nested list.
I want to use PHP to look at the folders and then display there names. How do i do this, or has it been done before?
Use RecursiveDirectoryIterator. Something like this:
$dir = new RecursiveDirectoryIterator('path/to/directory');
foreach (new RecursiveIteratorIterator($dir) as $filename => $file) {
if($file->isDir())
echo $filename . '<br/>';
}
Related
My directory structure is like D:/dir1/dir2/project_dir/dir3/dir4/dir5/cache/all_files
So i want to get all the files under cache folder.
So i wrote
glob("project_dir/*/*/*/cache/*");
But cache folder is also there in dir3 or dir4 like
D:/dir1/dir2/project_dir/dir3/dir4/dir5/dir6/dir7/cache/all_files
or
D:/dir1/dir2/project_dir/dir3/cache/all_files
So can anyone give me the regex to get the files from 'cache' folder,
like
glob("project_dir/*/cache/*");
Btw, this is not working because it is searching for immediate directory after project_dir.
Thanks in advance.
Hope this helps!
Supports PHP 5 and 7 only,check your version before using this code
for more info refer : http://php.net/manual/en/class.directoryiterator.php
$searchCacheFolderUnder = 'D:\xampp\htdocs\mygit\\';
$pathOfAllCacheFolders = array();
$dir = new DirectoryIterator(realpath($searchCacheFolderUnder));
foreach ($dir as $fileInfo) {
if($fileInfo->isDir()) {
// optimize strpos with inbuild directory iterator methods
if( strpos($fileInfo->getPathname(), 'cache') !== false ){
$pathOfAllCacheFolders[] = $fileInfo->getPathname();
}
}
}
// contains all cache folders path
echo "<pre>";
print_r($pathOfAllCacheFolders);
echo "</pre>";
// loop on all cache folders
foreach($pathOfAllCacheFolders as $cachePath)
$dirCache = new DirectoryIterator(realpath($cachePath));
foreach ($dirCache as $fileInfo) {
//$fileInfo object will have everything you need
echo "<pre>";
print_r($fileInfo);
echo "</pre>";
}
You have to modify this code a bit to meet your requirement. Refer Directory Iterator for more available options.
You can use glob('*') but the only perk for using this is it ignores all hidden files. If you want all the files that includes hidden files in a folder then use:
$files = glob('project_dir/cache/{,.}*', GLOB_BRACE);
Remember this will return all the files for "." and "" and even the directory entries . and .. Let me know in case of any issues.
Hope this helps.
I used php real path to get actual path of files and directory to delete and after deleted i will print all deleted
items. But my problem is that it also show the real path where the file source is and i don't want to show it to users
is there any way i can hide the pay and only show the file example.
I don't like it to look like this
[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg
[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/room_home_1.jpg
[Directory]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder
Is there anyway i can make it look this way using rejex or any method please i need help i have to remove /mnt/wef66/d2/81/557642661/htdocs/
[File]: www.example.com/useruploads/myfiles/imagefolder/mosaic_1.jpg
[File]: www.example.com/useruploads/myfiles/imagefolder/room_home_1.jpg
[Directory]: www.example.com/useruploads/myfiles/imagefolder
Maybe using something like this
echo preg_replace("/mnt/wef66/d2/81/557642661/htdocs", "www.example.com", "[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg");
$path = realpath($parentBas);
I would use configuration variables $privatePath and $publicPath.
So you can concat whichever you want to the relative paths to your directories or files.
For your example:
$privatePath = '/mnt/wef66/d2/81/557642661/htdocs/';
$publicPath = 'www.example.com/';
$pic1RelativePath = 'useruploads/myfiles/imagefolder/mosaic_1.jpg';
$pic1privatePath = $privatePath . $pic1RelativePath;
// /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg
$pic1publicPath = $publicPath . $pic1RelativePath;
// www.example.com/useruploads/myfiles/imagefolder/mosaic_1.jpg
I think this is easier and more efficient than replacing the paths with regex.
EDIT:
If you have all the real paths in an array, you can loop through it and replace easily all the private paths with the public paths this way:
$paths = [
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg',
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/room_home_1.jpg',
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder'
];
foreach ($paths as &$path) {
$path = str_replace($privatePath, $publicPath, $path);
}
print_r($paths);
I am looking for some help with my code, I have looked elsewhere but am having difficulty to really understand what is going on with the code given elsewhere and I am hoping someone can help me.
I have one gallery page that uses $_POST to change the folder the gallery gets it images form based on the link clicked.
What I want now is to code a search function that looks through them all for a string (a jpg) when it finds it, it returns its img tags and displays the image.
I am having trouble making scandir work and display currently using this code
<?php
$dir = "/galleries/images/adult-cakes/images/";
$scan = scandir($dir);
echo $dir;
print_r($scan);
foreach ($scan as $output) {
echo "$output" . "<br />";
}
?>
that returns the echo dir but nothing else ( please note print was something I tried it was echo before and neither is working.
Then I need to get the output of all the gallery types, adult, anniversary etc and put them into a loop like so
search criteria = cake 1(.jpg)
put scandir info into $folderarray
search this folder until found -
galleries/images/$folderarray/images/
loop
if found then echo img tags with link to pic
if not display not found
This will get an array of all the files in directory $dir
<?php
$dir = "/galleries/images/adult-cakes/images/";
$images = glob($dir . '*');
?>
Do this to get all subdirectories of $Dir into array $DirArray:
$Dir = '/galleries/images/'; //
foreach ( $DirArray = array_filter(glob($Dir . '*'), 'is_dir') as $DirName ) {
$DirName = str_replace($Dir, '', $DirName); // Optionally, remove path from name to display
echo "Dir Name: $DirName <br />\n"; // Test
}
echo var_dump($DirArray); // Test
Modify accordingly
I have a double question. Part one: I've pulled a nice list of pdf files from a directory and have appended a file called download.php to the "href" link so the pdf files don't try to open as a web page (they do save/save as instead). Trouble is I need to order the pdf files/links by date created. I've tried lots of variations but nothing seems to work! Script below. I'd also like to get rid of the "." and ".." directory dots! Any ideas on how to achieve all of that. Individually, these problems have been solved before, but not with my appended download.php scenario :)
<?php
$dir="../uploads2"; // Directory where files are stored
if ($dir_list = opendir($dir))
{
while(($filename = readdir($dir_list)) !== false)
{
?>
<p><a href="http://www.duncton.org/download.php?file=login/uploads2/<?php echo $filename; ?>"><?php echo $filename;
?></a></p>
<?php
}
closedir($dir_list);
}
?>
While you can filter them out*, the . and .. handles always come first. So you could just cut them away. In particular if you use the simpler scandir() method:
foreach (array_slice(scandir($dir), 2) as $filename) {
One could also use glob("dir/*") which skips dotfiles implicitly. As it returns the full path sorting by ctime then becomes easier as well:
$files = glob("dir/*");
// make filename->ctime mapping
$files = array_combine($files, array_map("filectime", $files));
// sorts filename list
arsort($files);
$files = array_keys($files);
In the script below, I'm attempting to iterate over the folders and files inside of the $base folder. I expect it to contain a single level of child folders, each containing a number of .txt files (and no subfolders).
I'm just needing to understand how to reference the elements in comments below...
Any help much appreciated. I'm really close to wrapping this up :-)
$base = dirname(__FILE__).'/widgets/';
$rdi = new RecursiveDirectoryIterator($base);
foreach(new RecursiveIteratorIterator($rdi) as $files_widgets)
{
if ($files_widgets->isFile())
{
$file_name_widget = $files_widgets->getFilename(); //what is the filename of the current el?
$widget_text = file_get_contents(???); //How do I reference the file here to obtain its contents?
$sidebar_id = $files_widgets->getBasename(); //what is the file's parent directory name?
}
}
//How do I reference the file here to obtain its contents?
$widget_text = file_get_contents(???);
$files_widgets is a SplFileInfo, so you have a few options to get the contents of the file.
The easiest way is to use file_get_contents, just like you are now. You can concatenate together the path and the filename:
$filename = $files_widgets->getPathname() . '/' . $files_widgets->getFilename();
$widget_text = file_get_contents($filename);
If you want to do something funny, you can also use openFile to get a SplFileObject. Annoyingly, SplFileObject doesn't have a quick way to get all of the file contents, so we have to build a loop:
$fo = $files_widgets->openFile('r');
$widget_text = '';
foreach($fo as $line)
$widget_text .= $line;
unset($fo);
This is a bit more verbose, as we have to loop over the SplFileObject to get the contents line-by-line. While this is an option, it'll be easier for you just to use file_get_contents.