I am using code that i found on here which works great but the moment i try to access a file in a sub-directory it just doesn’t want to work.
It gets a file, creates a temp file to write to, then looks for some text in the file and replaces that text with new text, then saves the updated file, then deletes the temp file.
The below works fine:
$reading = fopen('links.htm', 'r');
$writing = fopen('links.tmp', 'w+');
$replaced = false;
while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line,'::template::')) {
$line = "replacement line!\n";
$replaced = true;
}
fputs($writing, $line);
}
fclose($reading); fclose($writing);
// might as well not overwrite the file if we didn't replace anything
if ($replaced)
{
rename('links.tmp', 'links.htm');
} else {
unlink('links.tmp');
}
This doesnt work:
$reading = fopen('path/to/links.htm', 'r');
$writing = fopen('path/to/links.tmp', 'w+');
$replaced = false;
while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line,'::template::')) {
$line = "replacement line!\n";
$replaced = true;
}
fputs($writing, $line);
}
fclose($reading); fclose($writing);
// might as well not overwrite the file if we didn't replace anything
if ($replaced)
{
rename('path/to/links.tmp', 'path/to/links.htm');
} else {
unlink('path/to/links.tmp');
}
Any suggestions?
The path is absolute i defined it in the code earlier and use it to create file and write files to but when i want the code above to work in the same way it just doesn’t want to.
Also the folders permissions has been set to write and read which also works fine.
Ran the code in the sub folder and works fine but not from the top level directory.
Error reporting is turned off, turned it on now:
The process cannot access the file because it is being used by another process. (code: 32)
So after a week of looking around, testing, breaking and recoding a lot i found a simple way to do this.
Since most of the content is dynamically created as well as the links.htm file, it was quite hard finding where and when the file is accessed since the code accesses about 300 of my clients sites and updates the links file.
Simple fix:
There is probably easier ways to do this but this is what worked for me, hope it helps someone.