Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

PHP coding academy Page 25

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
seancyril



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

PostPosted: Wed Jul 28, 2010 11:40 am    Post subject: PHP coding academy Page 25 Reply with quote

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



?>
Back to top
View user's profile Send private message
Hudzilla
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 265
Location: LXF Towers

PostPosted: Thu Jul 29, 2010 8:30 pm    Post subject: Reply with quote

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:

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:

chdir("..");

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


Let me know if that fixes your problem.


Paul
Back to top
View user's profile Send private message Visit poster's website
seancyril



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

PostPosted: Fri Jul 30, 2010 3:43 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
leke
LXF regular


Joined: Mon Oct 22, 2007 6:45 pm
Posts: 503
Location: Oulu, Finland

PostPosted: Fri Jul 30, 2010 4:56 pm    Post subject: Reply with quote

I'm not so good at programming, but maybe it could be...
Quote:
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.
_________________
http://truecenterpublishing.com/zenstory/maybe.html
Back to top
View user's profile Send private message
seancyril



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

PostPosted: Thu Aug 05, 2010 8:55 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
seancyril



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

PostPosted: Mon Aug 09, 2010 1:45 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast