Jump to content

[SOLVED] change multiple files


dzedward

Recommended Posts

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.

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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.