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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.