Xu Wei Jie Posted March 6, 2009 Share Posted March 6, 2009 I wish to loop thru all the files in the directories and sub directories and change some content within them using string functions. Any idea how to go about it? Thanks Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi Recursive function will do it. Basically have a function that loops through a directory (using readdir), with the directory name passed to it as a parameter. Call it once with the root directory. For each "file" found check if it is a directory and if it is then call the function again but passing the newly found directories full name. In the function you can also change values if you want. Note that it will take a long time and potentially could cause a lot of damage if your editing went wrong. All the best Keith Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 7, 2009 Author Share Posted March 7, 2009 I wrote this function but it seems to have a problem with the directory at the bottom. It recognizes that directory as a file. Any one can help out to see where I go wrong? Thanks function TraverseDirectory($filedir,$change) { if ($handle = opendir($filedir)) { /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { if( is_dir($file) ) { $path = $filedir."/".$file; TraverseDirectory($path,$change); } else { $f = fopen($file,'r'); $contents = fread($f, filesize($file)); fclose($f); } } } closedir($handle); return true;; } Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Hi Cannot see anything obvious with it, but one potential error is that readdir will return both "." and "..". As such I think that you could potentially have an endless loop. Try this to get rid of that possibility function TraverseDirectory($filedir,$change) { if ($handle = opendir($filedir)) { /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { if( is_dir($file) ) { if ($file != '.' && $file !='..') { $path = $filedir."/".$file; TraverseDirectory($path,$change); } } else { $f = fopen($file,'r'); $contents = fread($f, filesize($file)); fclose($f); } } } closedir($handle); return true;; } All the best Keith Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 7, 2009 Author Share Posted March 7, 2009 I tried but it does not solve the problem. Weird thing is the directory at the bottom cannot be recognized as a directory nor file. hmmm Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Hi Is it just one particular file / directory that is triggering the problem? All the best Keith Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 7, 2009 Author Share Posted March 7, 2009 Yes it is 1 particular folder that does not get recognized as a directory. Nor is it a file. I tried is_file and is_dir on it but they can't recognize it. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Hi Err, just thought. Think is_dir needs a full path rather than just the name. So you would want if( is_dir($filedir."/".$file) ) All the best Keith Quote Link to comment Share on other sites More sharing options...
Xu Wei Jie Posted March 7, 2009 Author Share Posted March 7, 2009 Thanks Keith. It worked. I was careless with the relative addresses. =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.