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
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...

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.