PHP coding academy Page 25

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

PHP coding academy Page 25

Postby seancyril » Wed Jul 28, 2010 10:40 am

Hi there, I am working my way (very slowly!) through the PHP coding academy book in this months mag and have come to another dead end.
I am trying to do the scan of my home directory and all subdirectories and sending the output to an "output" file located in my home directory as described on Page 25
However when I run the command I get a whole load of data that just flies past on the command line-this is my file tree for my home directory being printed out. However this data is not sent to an "output" file in my home directory nor even is this file created. I must admit that I have stopped the process as it is taking such a long time to print out all of my file tree in the command line.
Can anyone look at the code that I have printed out and see if I have made any mistakes?

Many thanks
Sean

<?php
function subdir_scan($dir) {
$files=scandir($dir);
foreach($files as $file) {
if($file==".") continue;
if($file=="..") continue;


print"$dir/$file\n";


if(is_dir("$dir/$file")) {

subdir_scan("$dir/$file");

}


}


}

chdir("..");
subdir_scan(".");
chdir("..");

ob_start();
subdir_scan(".");
$output=ob_get_clean();
file_put_contents("output",$output);



?>
seancyril
 
Posts: 18
Joined: Sat Mar 13, 2010 12:08 pm

Postby Hudzilla » Thu Jul 29, 2010 7:30 pm

Hey!

The file isn't created because your script is actually running the scan twice - and you kill it while it's still running the first scan.

Here:

Code: Select all
chdir("..");
subdir_scan(".");
chdir("..");

ob_start();
subdir_scan(".");
$output=ob_get_clean();
file_put_contents("output",$output);


That's your problem. See, unless you run ob_start(), everything you print to the command line is printed out straight away. In line 1, you change up to the parent directory - your home directory - which is correct. You then start a scan before you've started the output buffering, which is why you see everything printed out. You then (on line three) change up another directory, into /home presumably. Eventually you start output buffering and run the scan. I think what you're looking to do is this:

Code: Select all
chdir("..");

ob_start();
subdir_scan(".");
$output=ob_get_clean();
file_put_contents("output",$output);


Let me know if that fixes your problem.


Paul
User avatar
Hudzilla
Site admin
 
Posts: 265
Joined: Mon Apr 04, 2005 11:52 am
Location: LXF Towers

Postby seancyril » Fri Jul 30, 2010 2:43 pm

Hi Paul, thanks for your reply I have changed the code so now it looks like this

<?php
function subdir_scan($dir) {
$files=scandir($dir);
foreach($files as $file) {
if($file==".") continue;
if($file=="..") continue;


print"$dir/$file\n";


if(is_dir("$dir/$file")) {

subdir_scan("$dir/$file");

}


}


}

chdir("..");

ob_start();
subdir_scan(".");
$output=ob_get_clean();
file_put_contents("output",$output);



?>
I am now getting alot of error messages which I've given a sample of below. Is ther something wrong with my foreach() loop. Also I don't understand the permission denied message on line 3.
I appreciate you taking time out to help me with this. It's quite tricky when things go wrong with this stuff and you haven't got anyone to give advice
Sean
in /home/sean/PHP/project1.php on line 3
PHP Warning: scandir(): (errno 13): Permission denied in /home/sean/PHP/project1.php on line 3
PHP Warning: Invalid argument supplied for foreach() in /home/sean/PHP/project1.php on line 4
PHP Warning: scandir(./.wine/dosdevices/c:/users/sean/My Documents/.wine/dosdevices/c:/users/sean/My Documents/.wine/dosdevices/c:/users/sean/My
seancyril
 
Posts: 18
Joined: Sat Mar 13, 2010 12:08 pm

Postby leke » Fri Jul 30, 2010 3:56 pm

I'm not so good at programming, but maybe it could be...
PHP Warning: scandir(): (errno 13): Permission denied in /home/sean/PHP/project1.php on line 3

Something to do with the set file permissions for that particular file or folder?
If this is the case, then it could lead to the foreach error because PHP expects something other than none in the loop.
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby seancyril » Thu Aug 05, 2010 7:55 pm

Thanks for your reply. My permissions for the PHP directory are that I can read, write to it, it is also executable. The project1.php folder itself I can read and write to. don't know what I can change here in the permissions?
Sean
seancyril
 
Posts: 18
Joined: Sat Mar 13, 2010 12:08 pm

Postby seancyril » Mon Aug 09, 2010 12:45 pm

Hi Paul, I am still not able to run the command without getting lots of error messages like those below. also the results of the scan are not being saved in an output directory. The new output directory is not being made at all
Sean


home/sean/.wine/dosdevices/z:/tmp/orbit-gdm): failed to open dir: Permission denied in /home/sean/PHP/project1.php on line 3
PHP Warning: scandir(): (errno 13): Permission denied in /home/sean/PHP/project1.php on line 3
PHP Warning: Invalid argument supplied for foreach() in /home/sean/PHP/project1.php on line 4
seancyril
 
Posts: 18
Joined: Sat Mar 13, 2010 12:08 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest