I'm using this code to get a listing of every folders/subfolders of a repertory :
$path = realpath($userdir);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
echo "$filename\n";
}
However it displays something like this, wich add dot or double dots and displays the folder twice :
C:\wamp\www\gg\ftp\repository\user\mister.
C:\wamp\www\gg\ftp\repository\user\mister..
C:\wamp\www\gg\ftp\repository\user\mister\apatik.
C:\wamp\www\gg\ftp\repository\user\mister\apatik..
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv.
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv..
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx.
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx..
Instead of something cleaner like this :
C:\wamp\www\gg\ftp\repository\user\mister
C:\wamp\www\gg\ftp\repository\user\mister\apatik
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx
Is there a way ? I was previously using the function glob
glob($sub . '/*' , GLOB_ONLYDIR);
which was displaying the folder correctly but i couldn't get it to be recursive to display subfolders aswell.
Thanks
you need something like this
set_time_limit(0);
function scanDirectory($sub = ''){
$folders = glob($sub . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
echo "$folder<br />";
scanDirectory($folder);
}
}
scanDirectory();
and this will list all folders on current drive
EDIT
$folders = glob($sub . '/*' , GLOB_ONLYDIR); will get all the folders in specified directory.
foreach($folders as $folder) will loop over folders array.
$sub will be the folder name that will be explored.
so, these are the folders
A B C
$folders will have like $folders['A', 'B', 'C']
and in loop, each A B and C will be passed as $sub to check either it has more folders in it or not.
Related
with this code I get filename from the current folder
<?php
$mydir = dirname(__FILE__);
$myfiles = array_diff(scandir($mydir), array('.', '..'));
$arrsingleresult = str_replace('.php', '', $myfiles);
print_r($arrsingleresult);
?>
Now how can I get filenames from one folder back ? (../ like this) any work around ?
Please use double time dirname() function to back one level
dirname(dirname(__FILE__));
See previous Question which was partly answered but there has been a change in requirements for the script: PHP - Exploding / Moving / Filename
i'm new to php and am stuck. I have loads of files that look like this:
2014-04-01 NS122345 - The date, the initials of the person and there employee code.
I want to be able to move the files that have NS or JB Or GA into there relevant folder/directories. So for NS it would go into the Nathan Saunders Folder, for JB into the Joe Bailey folder.
My directory structure looks like this:
root/wan/upload - Where files/images/docs are stored. Inside upload folder i have:
>2014-04-08 NS6565.doc
>2012-01-03 JB8932.doc
>2013-02-01 GA5434.doc
>etc
root/wan/administrator/components/com_upload - where my code is stored
This is my php code for moving, creating and checking the filename and putting it in the correct folder:
$dir = JPATH_BASE . DS . "upload";
$folders = array('SE528733B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/528733B','SE125673B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/125673B','SE3452312'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/3452312');
$files = scandir($dir);
foreach($files AS $file){
if(!is_file($dir.DS.$file)){ continue; }
$array = explode(' ', $file);
if(count($array)<2){ continue; }
$firstTwoLetters = substr($array[1], 0, 9);
$foldername = $firstTwoLetters;
if(is_dir($folders[$firstTwoLetters])||mkdir($foldername[$firstTwoLetters],0777, 1))
rename($dir.DS.$file,$foldername[$firstTwoLetters].DS.$file);
That code currently reads the filename if its already in the array "folders" it moves to the correct folder, I have changed it recently to make the folder automatically reading whatever file is in the upload section, but the problem comes when making the folder, the mkdir seems to make the directory:
1) in the wrong place it makes it where the code is stored which is in the com_upload section instead of making it in the upload folder.
2) Names it wrong it takes the first letter not the letters or numbers after it. E.g. "2014-04-08 NS6565.doc", makes the directory "N"
Any help to fixing those 2 problems would be great.
Thanks,
1) If you want to create the directory in another place or you use a relative path from the directory your code is or you use an absolute path.
2) When you are creating the directory you use $foldername but it isn't a name of any directory. It's instead the name of the file. Also, you use it as an array when it's a string (so it only take one char)
Try this:
$dir = JPATH_BASE . DS . "upload";
$folders = array('SE528733B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/528733B','SE125673B'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/125673B','SE3452312'=>'/var/www/vhosts/test.cariss.co.uk/httpdocs/wan/upload/3452312');
$files = scandir($dir);
foreach($files AS $file){
if(!is_file($dir.DS.$file)){ continue; }
$array = explode(' ', $file);
if(count($array)<2){ continue; }
$firstTwoLetters = substr($array[1], 0, 9);
$foldername = substr($firstTwoLetters,0,2);
if(is_dir($dir. DS . $foldername)||mkdir($dir. DS . $foldername,0777, 1))
rename($dir.DS.$file,$dir . DS . $foldername . DS.$file);
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/>';
}
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 would like to delete set of directories but 1st I'm gonna have to get the directories names so suppose I've the following site www.my_site.com and would use the following code to get all directories names.
$get_dirs = glob("*", GLOB_ONLYDIR);
for ($i=0;$i<count($get_dirs);$i++){
echo $get_dirs[$i].'+'; // Will show results divided by + sign
}
suppose the results as following (if I've 5 directories and note it divided by + sign)
dir1+dir2+dir3+dir4+dir5+
to use it
rrmdir(dir1); // that would delete only directory dir1
My Question
How to explode the results of directories names dir1+dir2+dir3+dir4+dir5+ based on the + sign and loop using delete function on all so finally all directories dir1 and dir2 and dir3 and dir4 and dir5 are deleted.
I think this is what your trying to do?
$dir = trim('dir1+dir2+dir3+dir4+dir5+', '+');
$arr = explode("+", $dir);
foreach ($arr as $a){
rrmdir($a);
}
You have a extra '+' at the end, so trim($val ,'+')
You answered Your own question there actually:
$dirs = explode('+', $get_dirs);
foreach ($dirs as $dir) {
rrmdir($dir);
}
or did i understand something wrong? And why don't you do it already in for cycle?