I need to update the same line that also includes the date in the format dd/mm/yyyyalong with some line in the file group. I checked the answers here to similar questions, but could not make any of the proposed templates in my code.
My current PHP code is:
<?php
$sysdate = date("d/m/Y");
$dir = opendir($argv[1]);
$files = array();
while (($file = readdir($dir)) !== false) {
$files[] = $file;
}
closedir($dir);
sort($files);
foreach ($files as $file) {
$lines = '';
if (strpos($file,".hql") != false || strpos($file,".HQL") != false)
{
$procfile = $argv[1] . '\\' . $file;
echo "Converting filename: " . $procfile . "\n";
$handle = fopen($procfile, "r");
$lines = fread($handle, filesize($procfile));
fclose($handle);
$string = $lines;
$pattern = '[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]';
$replacement = $sysdate;
$lines = preg_replace($pattern, $replacement, $string);
echo $lines;
$newhandle = fopen($procfile, 'w+');
fwrite($newhandle, $lines);
fclose($newhandle);
}
}
closedir($dir);
?>
When I run this code on the command line, it does not report an error and seems to work correctly. But as soon as it finishes and I check my files, I see that the contents of each file are deleted, and all of them become 0 KB files, in which there is nothing.
source
share