By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,990 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,990 IT Pros & Developers. It's quick & easy.

Sending an array ouput to an text file

P: 3
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($directory, $filter=FALSE)
{
// if the path has a slash at the end we remove it here
if(substr($directory,-1) == '/')

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

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

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

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

elseif(is_readable($directory))

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

while (FALSE !== ($file = readdir($directory_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=fileowner($path);
$fileownerarray=posix_getpwuid($fileowneruid);
$fileowner=$fileownerarray['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($subdirectories),
'kind' => 'directory',
'file owner' => $fileowner,
'permission' =>$info,


// we scan the new path by calling this function

'content' => scan_directory_recursively($path, $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($subdirectories)));

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

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

$fileowneruid=fileowner($path);
$fileownerarray=posix_getpwuid($fileowneruid);
$fileowner=$fileownerarray['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($subdirectories),
'file owner' => $fileowner,
'permission' =>$info);

}

}

}

}

}

// close the directory

closedir($directory_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('directory', 'extension');

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

//echo "</pre>";

$fp=fopen("result.txt","a");
foreach ($a as $key=>$dataArray) {
fwrite($fp, "[$key]\n");

foreach ($dataArray as $k => $v) {
fwrite($fp, "$k=$v\n");
}
fwrite($fp, "\n");
}
fclose($fp);
[/php]
Jan 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: 33
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 :)
Jan 14 '07 #2

P: 3
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
Jan 20 '07 #3

P: 33
Hi ahmed

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


Subash
Jan 21 '07 #4

P: 3
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
Feb 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.