dzedward Posted December 28, 2007 Share Posted December 28, 2007 Is there a php script that will run through and find a certain file name on a server, and change a string in it? Not just in the same directory but across all directories?? Thanks Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/ Share on other sites More sharing options...
GingerRobot Posted December 28, 2007 Share Posted December 28, 2007 Sure. Modified something i previously had: <?php function find_file($dir,$file_to_find,$text_to_find,$replacement){ $no_of_files=0; $handler = opendir($dir); while(false !== ($file = readdir($handler))){ if($file != '.' && $file != '..'){ if(is_dir($dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined $sub_dir = $dir.'\\'.$file; find_file($sub_dir,$file_to_find,$text_to_find,$replacement); }else{ if($file==$file_to_find){//this is the file we are looking for $new_contents = str_replace($text_to_find,$replacement,file_get_contents($dir.'\\'.$file)); $h = fopen($dir.'\\'.$file,'w'); fwrite($h,$new_contents); fclose($h); } } } } } echo find_file('C:\wamp\www','test.txt','test','blah'); ?> Would find all files called test.txt in the folder C:\wamp\www and any sub directories, and replace all instances of test with blah. Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424854 Share on other sites More sharing options...
dzedward Posted December 28, 2007 Author Share Posted December 28, 2007 if i want it to start in the same directory as the file, would i change "C:\wamp\www" to "/" Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424868 Share on other sites More sharing options...
GingerRobot Posted December 28, 2007 Share Posted December 28, 2007 Change it to: $_SERVER['DOCUMENT_ROOT'] Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424875 Share on other sites More sharing options...
dzedward Posted December 28, 2007 Author Share Posted December 28, 2007 Parse error: syntax error, unexpected T_STRING in /hermes/web02/b1963/dzedward/changeAll.php on line 21 Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424881 Share on other sites More sharing options...
GingerRobot Posted December 28, 2007 Share Posted December 28, 2007 Call me old fashioned, but you could always try showing the code in question. We're not mind readers here! Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424888 Share on other sites More sharing options...
dzedward Posted December 28, 2007 Author Share Posted December 28, 2007 lol sorry, just thought you would know since you wrote it echo find_file('$_SERVER['DOCUMENT_ROOT']','findme.txt','max_execution_time = 30','max_execution_time = 60'); Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424889 Share on other sites More sharing options...
GingerRobot Posted December 28, 2007 Share Posted December 28, 2007 Yeah, the point is that you may change it in a different way. You don't need the quotes around it: find_file($_SERVER['DOCUMENT_ROOT'],'findme.txt','max_execution_time = 30','max_execution_time = 60'); Also, there's no point echoing it - it doesn't return anything. Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424890 Share on other sites More sharing options...
dzedward Posted December 28, 2007 Author Share Posted December 28, 2007 so lets say my domain is www.mydomain.com. would i change it to find_file($_SERVER['http://www.mydomain.com/'],'findme.txt','max_execution_time = 30','max_execution_time = 60'); Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424897 Share on other sites More sharing options...
GingerRobot Posted December 28, 2007 Share Posted December 28, 2007 No. You leave it is as $_SERVER['DOCUMENT_ROOT']. Its a predefined constant. See: http://uk3.php.net/reserved.variables. It contains the directory of the script currently being executed. Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424901 Share on other sites More sharing options...
dzedward Posted December 28, 2007 Author Share Posted December 28, 2007 ok, here are errors now.. i chmod file to 777 Warning: file_get_contents(/home/users/web/b1963/dzedward\findme.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /hermes/web02/b1963/dzedward/changeAll.php on line 12 Warning: fopen(/home/users/web/b1963/dzedward\findme.txt) [function.fopen]: failed to open stream: Permission denied in /hermes/web02/b1963/dzedward/changeAll.php on line 13 Warning: fwrite(): supplied argument is not a valid stream resource in /hermes/web02/b1963/dzedward/changeAll.php on line 14 Warning: fclose(): supplied argument is not a valid stream resource in /hermes/web02/b1963/dzedward/changeAll.php on line 15 Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424905 Share on other sites More sharing options...
GingerRobot Posted December 28, 2007 Share Posted December 28, 2007 Try: <?php function find_file($dir,$file_to_find,$text_to_find,$replacement){ $no_of_files=0; $handler = opendir($dir); while(false !== ($file = readdir($handler))){ if($file != '.' && $file != '..'){ if(is_dir($dir.'/'.$file)){//if this is a directory, recall the function with the subdirectory defined $sub_dir = $dir.'/'.$file; find_file($sub_dir,$file_to_find,$text_to_find,$replacement); }else{ if($file==$file_to_find){//this is the file we are looking for echo 'found'; $new_contents = str_replace($text_to_find,$replacement,file_get_contents($dir.'/'.$file)); $h = fopen($dir.'/'.$file,'w'); fwrite($h,$new_contents); fclose($h); } } } } } echo find_file($_SERVER['DOCUMENT_ROOT'],'test.txt','test','blah'); ?> Wrote this on a local windows machine, hence the back slashes. Might be an issue with that, so i've changed them all to forward slashes. Give that a go. Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424908 Share on other sites More sharing options...
dzedward Posted December 28, 2007 Author Share Posted December 28, 2007 yes, that has done the trick. Thank you very very much. Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424909 Share on other sites More sharing options...
GingerRobot Posted December 28, 2007 Share Posted December 28, 2007 Good stuff. Glad we got there in the end. Link to comment https://forums.phpfreaks.com/topic/83505-solved-change-multiple-files/#findComment-424910 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.