Jump to content

Sub-Directory listing with files


karthikeyan_coder

Recommended Posts

Hello,

 

        I need to list a directory's all sub-folders and its files...

 

i've tried for directory listing alone...

$list_ignore = array ('.','..');
$main_trace_dir = "uploads";
$handle=opendir($main_trace_dir);
while ($file = readdir($handle)) {
  if (is_dir($file) && !in_array($file,$list_ignore)) {    
  echo $file;
  }
   }
    closedir($handle);

 

lets say the directory structure is...

 

uploads

|______Subfolder A

|          |_________Subfolder 1

|          |              |__________Some file.zip

|          |              |__________Some other file.mp

|          |_________Subfolder 2

|                          |_________file.

|______Subfolder B

|          |_________X

|                          |________

|some file in main directory.zip

 

actually the number of files and subfolders of "uploads" is numerous...

 

Current code is trying to list just "Subfolder A, Subfolder B" ... actually i have to extract *.zip files and move the contents to some directory like "extracted". if it is not zip then just move to "extracted"... i dont know how to execute subfolders/files section... any help?

 

                         

Link to comment
https://forums.phpfreaks.com/topic/39239-sub-directory-listing-with-files/
Share on other sites

have to Extract if it is zip and move. else just move

 

if current file is a zip then

______________________

 

example content of a zip

somefile.zip

--------------> titleimage.jpg

-------------->song.mp3

-------------->song2.mp3

 

someother.zip

------------->song3.mp3

-------------->song4.mp3

 

 

if the current file is a zip then we have to extract and move the files. if the extracted file is an image then ... /uploads/images else /uploads/songs

 

if the current file is a *.mp3 then

____________________________

just move to /uploads/songs

 

 

i've tested that.. unzip command is working... i dont know how to move after extraction...

 

1.download a free shell for windows or linux.

 

2.make three folders or as meny needed

 

4.use the command exec to execute the new shell program to unzip all ziped files into a temp folder.

 

5. use a for loop to go throw the files to detect exstention's.

 

6. within the for loop use move_uploaded_file to the destination of the file enstention.

 

there you go.

I was thinking something like this. This will only work if you have PHP 5 though. Scandir isn't supported for 4.

 

<?php
//Array of content
$files = scandir($dir);
//Get rid of . and ..
$files = array_slice($files, 2);
//Loop for the number of files/contents
for($x = 0; $x < count($files); $x++)
{
//This should be a little bit more advanced to get rid of other extensions so you are left with only directories, but you get the point
        if(preg_match("/.mp3/", $files[$x]))
{
	//Move
                rename("/oldpath/oldpath/oldpath/".$files[$x], "/newpath/newpath/newpath/".$files[$x]);
	//Take them out of the array
                unset($files[$x]);
}
} 
?>

 

Now the $files array only has directories left (technically, you will need to do more searching to weed out the rest). I would probably consider making what I did above into a function so you can continually loop to go through all the directories.

 

I am unsure as to how decent my code is. In fact I would assume its poor. I know it works, but the goal was just to get you some ideas. Sorry if it is useless.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.