-2

Possible Duplicates:
PHP recursive directory path
PHP Get all subdirectories of a given directory
PHP: Get list of all filenames contained within my images directory

I have a directory on the server which contains some sub folders. The sub folders contain some pdf files. I need to display all sub folder names and sub folder data as well. Please guide me how to read data in subfolders.

here is my code

<?php
$number=$_REQUEST['Username'];
$folder=dir("UserIds/".$Username."/");
$i=0;
while($folderEntry=$folder->read())
{
if($folderEntry !=".." && $folderEntry !=".")
      {   
      $message[$i]="http://www.myappdemo.com/appinstaller/UserIds/$number/".$folderEntry;
      $i++;
      }
  }
echo json_encode($message);
$folder->close();
?>

here I am enter the directory name I need that particular directory data will display
please guide me

Thanks for advance...

Community
  • 1
  • 1

3 Answers3

2

in recursive way.

$message=get_files("UserIds/".$Username);
function get_files($dir){
    $message=array();

    $folder=dir($dir);
    while($folderEntry=$folder->read())
    {
       if($folderEntry !=".." && $folderEntry !=".")
       {   

          $message[]="http://www.myappdemo.com/appinstaller/".$dir."/".$folderEntry;
          if (is_dir($dir."/".$folderEntry)){
             $new_message=get_files($dir."/".$folderEntry);
             if (is_array($new_message)){
                 $message=array_merge($message,$new_message);
             }
          }
       }
    }
    $folder->close();
  return $message;
}
Dreaded semicolon
  • 2,274
  • 1
  • 28
  • 43
  • thanks for your response I put your code i get this answer ["http:\/\/www.myappdemo.com\/appinstaller\/UserIds\/\/Winter.jpg"] here after userId missing sub folder name Warning: array_merge() [function.array-merge]: Argument #2 is not an array in /home/myappdemo/myappdemo.com/appinstaller/Directory.php on line 19 null – user883594 Aug 12 '11 at 12:37
  • I edited my message to add if array and also correct the folder name, you probably need to change it to reflect the message you want to display. I only meant to post the idea. also I'm not sure why you got array_merge problem did you post exactly or added new return? but anyway I put if is_array just in case – Dreaded semicolon Aug 12 '11 at 13:45
0

If you replace your

while($folderEntry=$folder->read())
{
     if($folderEntry !=".." && $folderEntry !=".")
     {   
         $message[$i]="http://www.myappdemo.com/appinstaller/UserIds/$number/".$folderEntry;
         $i++;
     } 
}

with

while (false !== ($folderEntry = $folder->read())) {
    if($folderEntry !=".." && $folderEntry !=".")
    {   
        $message[$i]="http://www.myappdemo.com/appinstaller/UserIds/$number/".$folderEntry;
        $i++;
    }
}

I think you're there.

And I can't see your setting $Username anywhere I reckon $folder=dir("UserIds/".$Username."/"); should be $folder=dir("UserIds/".$number."/"); as well.

johangu
  • 824
  • 6
  • 12
  • ho..thanks for your response yes it's $number here i am username i get the directory data but i need also sub folders data also plz guide me how to get sub folders data – user883594 Aug 12 '11 at 12:19
  • above code I get response is ["http:\/\/www.myappdemo.com\/appinstaller\/UserIds\/kumareawqwe\/2011-August-12"] here "2011-August-12" contains the sub folder i need sub folders data also plz tell how to get this – user883594 Aug 12 '11 at 12:25
  • Sorry, I read your original message a bit to hasty. I would break the whole thing out to a recursive method that checks the $folderEntry with is_dir (http://www.php.net/manual/en/function.is-dir.php) to solve the subfolder part. – johangu Aug 12 '11 at 12:30
-1

Ever heard of recursivity? :)

http://www.codingforums.com/showthread.php?t=71882

Catalin
  • 858
  • 5
  • 16