Sending an array ouput to an text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tasahmed
    New Member
    • Jan 2007
    • 3

    Sending an array ouput to an text file

    Hello Friends,

    I wrote a function which scans the current working directory and lists out details such as directory/file owner, permission etc. The output of this script can be viewing in the monitor when I give the command print_r which is printed out in a array format.

    The problem I facing is I want this output in a text file, but I have tried so many things it is not working I am copying my code below, I need help in this urgently. Thanks for the help in advance.

    Tas.

    [php]
    $d=getcwd();

    function scan_directory_ recursively($di rectory, $filter=FALSE)
    {
    // if the path has a slash at the end we remove it here
    if(substr($dire ctory,-1) == '/')

    {
    $directory = substr($directo ry,0,-1);
    }

    // if the path is not valid or is not a directory ...

    if(!file_exists ($directory) || !is_dir($direct ory))

    {
    // ... we return false and exit the function
    return FALSE;
    // ... else if the path is readable
    }

    elseif(is_reada ble($directory) )

    {
    // we open the directory
    $directory_list = opendir($direct ory);
    // and scan through the items inside

    while (FALSE !== ($file = readdir($direct ory_list)))

    {

    // if the filepointer is not the current directory

    // or the parent directory

    if($file != '.' && $file != '..')

    {

    // we build the new path to scan

    $path = $directory.'/'.$file;



    // if the path is readable


    if(is_readable( $path))

    {

    // we split the new path by directories


    $subdirectories = explode('/',$path);

    // if the new path is a directory
    $fileowneruid=f ileowner($path) ;
    $fileownerarray =posix_getpwuid ($fileowneruid) ;
    $fileowner=$fil eownerarray['name'];
    $groupid = posix_getegid($ path);
    $groupinfo['name'] = posix_getgrgid( $groupid);

    $perms = fileperms($path );

    if (($perms & 0xC000) == 0xC000) {
    // Socket
    $info = 's';
    } elseif (($perms & 0xA000) == 0xA000) {
    // Symbolic Link
    $info = 'l';
    } elseif (($perms & 0x8000) == 0x8000) {
    // Regular
    $info = '-';
    } elseif (($perms & 0x6000) == 0x6000) {
    // Block special
    $info = 'b';
    } elseif (($perms & 0x4000) == 0x4000) {
    // Directory
    $info = 'd';
    } elseif (($perms & 0x2000) == 0x2000) {
    // Character special
    $info = 'c';
    } elseif (($perms & 0x1000) == 0x1000) {
    // FIFO pipe
    $info = 'p';
    } else {
    // Unknown
    $info = 'u';
    }

    // Owner
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ?
    (($perms & 0x0800) ? 's' : 'x' ) :
    (($perms & 0x0800) ? 'S' : '-'));

    // Group
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ?
    (($perms & 0x0400) ? 's' : 'x' ) :
    (($perms & 0x0400) ? 'S' : '-'));

    // World
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ?
    (($perms & 0x0200) ? 't' : 'x' ) :
    (($perms & 0x0200) ? 'T' : '-'));

    if(is_dir($path ))

    {

    // add the directory detailsfile:///usr/share/doc/HTML/index.html to the file list

    $directory_tree[] = array(

    'path' => $path,
    'name' => end($subdirecto ries),
    'kind' => 'directory',
    'file owner' => $fileowner,
    'permission' =>$info,


    // we scan the new path by calling this function

    'content' => scan_directory_ recursively($pa th, $filter));


    // if the new path is a file


    }

    elseif(is_file( $path))


    {

    // get the file extension by taking everything after the last dot

    $extension = end(explode('.' ,end($subdirect ories)));

    // if there is no filter set or the filter is set and matches

    if($filter === FALSE || $filter == $extension)

    $fileowneruid=f ileowner($path) ;
    $fileownerarray =posix_getpwuid ($fileowneruid) ;
    $fileowner=$fil eownerarray['name'];
    $groupid = posix_getegid($ path);
    $groupinfo['name'] = posix_getgrgid( $groupid);

    $perms = fileperms($path );

    if (($perms & 0xC000) == 0xC000) {
    // Socket
    $info = 's';
    } elseif (($perms & 0xA000) == 0xA000) {
    // Symbolic Link
    $info = 'l';
    } elseif (($perms & 0x8000) == 0x8000) {
    // Regular
    $info = '-';
    } elseif (($perms & 0x6000) == 0x6000) {
    // Block special
    $info = 'b';
    } elseif (($perms & 0x4000) == 0x4000) {
    // Directory
    $info = 'd';
    } elseif (($perms & 0x2000) == 0x2000) {
    // Character special
    $info = 'c';
    } elseif (($perms & 0x1000) == 0x1000) {
    // FIFO pipe
    $info = 'p';
    } else {
    // Unknown
    $info = 'u';
    }

    // Owner
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ?
    (($perms & 0x0800) ? 's' : 'x' ) :
    (($perms & 0x0800) ? 'S' : '-'));

    // Group
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ?
    (($perms & 0x0400) ? 's' : 'x' ) :
    (($perms & 0x0400) ? 'S' : '-'));

    // World
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ?
    (($perms & 0x0200) ? 't' : 'x' ) :
    (($perms & 0x0200) ? 'T' : '-'));

    {

    // add the file details to the file list

    $directory_tree[] = array(

    'path' => $path,
    'name' => end($subdirecto ries),
    'file owner' => $fileowner,
    'permission' =>$info);

    }

    }

    }

    }

    }

    // close the directory

    closedir($direc tory_list);



    // return file list

    return $directory_tree ;


    // if the path is not readable ...

    }
    else
    {

    // ... we return false

    return FALSE;

    }

    }

    // to use this function to get all files and directories in an array, write:

    //$filestructure = scan_directory_ recursively('$d ');

    // to use this function to scan a directory and filter the results, write:

    // $fileselection = scan_directory_ recursively('di rectory', 'extension');

    // example
    //echo "<pre>";
    $a=print_r (scan_directory _recursively($d ));

    //echo "</pre>";

    $fp=fopen("resu lt.txt","a");
    foreach ($a as $key=>$dataArra y) {
    fwrite($fp, "[$key]\n");

    foreach ($dataArray as $k => $v) {
    fwrite($fp, "$k=$v\n");
    }
    fwrite($fp, "\n");
    }
    fclose($fp);
    [/php]
  • subash
    New Member
    • Sep 2006
    • 32

    #2
    Hi Ahemad,

    You don't want to place the whole code into this forum, You can use many array function to write the values into the text file OR you can get many code libraries from the sites like phpclasses.org


    Subash :)

    Comment

    • tasahmed
      New Member
      • Jan 2007
      • 3

      #3
      Originally posted by subash
      Hi Ahemad,

      You don't want to place the whole code into this forum, You can use many array function to write the values into the text file OR you can get many code libraries from the sites like phpclasses.org


      Subash :)
      Thanks subhash, I managed to get the output.
      Taslim

      Comment

      • subash
        New Member
        • Sep 2006
        • 32

        #4
        Hi ahmed

        How can you get the output - please share with us, it would be helpful for future


        Subash

        Comment

        • tasahmed
          New Member
          • Jan 2007
          • 3

          #5
          Originally posted by subash
          Hi ahmed

          How can you get the output - please share with us, it would be helpful for future


          Subash
          Hello subhash,

          Sorry,I did'nt see your post, now only I saw your post and hence the dely in answer,

          As you can see in the above code I was trying to get the array output after the curly braces were closed, so I used fopen to open a new file and fwrite within the curly braces of each file listing and directory listing array and then it worked. If you have any doubts in this I will post the corrected code.

          Cheers,
          Ahmed

          Comment

          Working...