开发者

how to read subfolders data in php? [duplicate]

This question already has answers here: Closed 11 years ago.

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


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;
}


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.


Ever heard of recursivity? :)

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜