Help - Search - Members - Calendar
Full Version: PHP and File Locking...
Hostony Board > General Support > PHP/MySQL
darcyelliott
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
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
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
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
Please give me URL of the script that don't want to work.
darcyelliott
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
Any time. wink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.
IPS Driver Error

IPS Driver Error

There appears to be an error with the database.
You can try to refresh the page by clicking here