darcyelliott
May 7 2004, 03:21 AM
Ok... I have been looking at php.net and stuff and I have a question dealing with file locking....
when I try and do an fopen on a file that another script has a file lock on for writing, what is returned just false or is it false and then some sort of error differenation between trying to open a locked file vs trying to open a no existant file.
I want to stick my f-open in a loop that will keep trying to open the file if it is locked, but if there is no way to tell the difference between a locked error and a non exists error, I am not sure how I will be able to do that.
Thanks in advance
Darce
Niakie
May 7 2004, 03:48 AM
What you should do is use file_exists() first (I would also suggest using is_file() just to make sure)... Other useful functions would be is_readable() and is_writeable()...
darcyelliott
May 7 2004, 04:12 AM
Yeah I had a brain storm (more like a brain fart) after i wrote the post...
I try and do the fopen, and if I fail, I do the fexists, if that passes i assume that the file has been locked for writing so I can't use it, I have another fopen in a loop until sucessful.... I probably need some more checking to make sure I don't get stuck in a loop, or come up with a max count to retry...
Thanks
Darce
Niakie
May 7 2004, 04:42 AM
I don't use file locking myself (90% of my work is database) but when I was programming in C I used to use something like this: (converted to PHP)
CODE
<?php
$file = '/dir/file.ext';
if(file_exists($file) && is_file($file)) {
for($i=0;$i<20;$i++) {
if(is_writeable($file)) {
//... file open stuff here
}
}
}
?>
Probably a better way to do it, but thats what I would suggest....
phobos
May 7 2004, 11:42 AM
Please give me URL of the script that don't want to work.
darcyelliott
May 7 2004, 01:40 PM
QUOTE
Please give me URL of the script that don't want to work.
Script works fine... I just was looking for different ways of thinking when using file locking
QUOTE
don't use file locking myself (90% of my work is database) but when I was programming in C I used to use something like this: (converted to PHP)
I would have much rather used a table and would have been more logical... but not my choice....
Thanks for your help
Darce
Alexandre
May 7 2004, 05:41 PM
Any time.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.