473,574 Members | 3,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sending an array ouput to an text file

3 New Member
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]
Jan 13 '07 #1
4 2285
subash
33 New Member
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
tasahmed
3 New Member
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
subash
33 New Member
Hi ahmed

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


Subash
Jan 21 '07 #4
tasahmed
3 New Member
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

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

Similar topics

1
3517
by: Kyle Maddison | last post by:
Hi, I would like to access an array via an index I have in a string. I know I can pull the string apart and manually access it but that just looks ugly to me :) $pg = "foo@bar.com"; $row->name = ""; //single quotes on 'owner' haven't helped $value = "pg".$row->name; echo $value; //ouput: 'pg'
1
2467
by: Frits v/d Laan | last post by:
Hi I cannot figure out how to import an array from PHP into Javascript MyPHPcode is like this <?php $i=0; While ($i < $num) { $PHPvar= (" $PHPvar \" $Mystring \" , "); $i++;
9
4888
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim strReturnData As String = _ System.Text.Encoding.ASCII.GetString(receiveBytes)
2
22587
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the...
2
12158
by: =?Utf-8?B?QWRl?= | last post by:
HI All, I am encountering the following error when I try to send an email through a SMTP server. I believe the problem lies with the authentication part when the network crednetials are used, error is thrown at the .send point. Error is: The following error occured Sending an email: System.ApplicationException: An error occurred sending...
23
6901
by: cmdolcet69 | last post by:
How can i add a 5ms delay between bytes i send over to a controller? I have to send 6 bytes in total however in between each byte i need to have a 5ms delay.
0
2871
by: Charles Crawford | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection mapped to a serial port. The application I'm writing runs on a Windows Mobile 5 device, so there's no built in printer support and I have to use a...
0
3754
by: =?Utf-8?B?Q2hhcmxpZQ==?= | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection mapped to a serial port. The application I'm writing runs on a Windows Mobile 5 device, so there's no built in printer support and I have to use a...
2
9776
by: ksheerasagar17 | last post by:
Hello All, Scenario: Sending an image through webservice as byte array to an Java webservice. The Problem1: The webservice method image property expects (data type) SByte rather than Byte array. Thus i'm converting a byte array to sbyte array and sending through web service. The converted SByte contains negative numbers wihch are resulting...
0
7764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8273
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5658
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5336
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3775
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.