PHP - How to replace a string in a very large number of files?
I have two million text files on a server that is accessible to internet users. I was asked to make changes (line replace operation) to these files as soon as possible. I was thinking about creating str_replace
for every text file on the server. However, I don't want to bind the server and make it inaccessible to Internet users.
Do you think this is a good idea?
<?php
ini_set('max_execution_time', 1000);
$path=realpath('/dir/');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
set_time_limit(100);
//do str_replace stuff on the file
}
source to share
Use find , xargs and sed from shell
, that is:
cd /dir
find . -type f -print0 | xargs -0 sed -i 's/OLD/NEW/g
Will search all the files recursively (hidden as well) within the current dir
and replace OLD
on NEW
using sed
.
Why -print0
?
From man find :
If you are piping the search output into another program and there is a very slight chance that the files you are looking for may contain a newline, then you should seriously consider using the '-print0' option instead of '-print'.
Why xargs
?
From man find :
The specified command runs once for each associated file.
That is, if /dir
there are 2000 files, it find ... -exec ...
will result in 2000 calls sed
; whereas it find ... | xargs ...
will only call sed
once or twice.
source to share