• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

have function to check file whether are opening

have function to check a file whether are open?
0
gameboss
Asked:
gameboss
  • 7
  • 4
  • 3
  • +2
1 Solution
 
us111Commented:
hmm I don't know a function which tell you if the file is opened
Try that:

<?
  $f = fopen("myfile.txt", "r");
  if ($f <> false)
       print "The file is opened";
?>
0
 
maxkirCommented:
No.
0
 
mrvithanCommented:
I amn't quite sure but it may depend on the software. If it locks the file, of couse you can't open it.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gamebossAuthor Commented:
I want use it in a multi users system.
multi user want write same file ,
when one user open a file,other user can get a message and exec another program,I do not want other user get  error message.
0
 
us111Commented:
well I have no idea how we can do that.......
Create a database with filename and opened as fields.

if an user opens a file : update the database with filename and opened=true
on close: opened=false

if an user opens a file which is already opened : check the filename in the database.

It could be a solution
0
 
gravityCommented:
Some pseudo code (how *do* you spell pseudo ?)

Check to see if lock file exists and put result in $open
while ($open==false) {
write lock file
open file
do whatever with file
close file
unlink lock file
check to see if lock file exists and put result in $open
}

Hope that enlightens.
0
 
us111Commented:
gravity has right too
0
 
maxkirCommented:
Try this code, i didn't test it.

$fp = fopen($filename, "w");

$attempts = 0
while ( !flock($fp, 2) && ($attempts++ < 10) )
{
   // file is locked by someone else
   sleep(1);
}

if ($attempts == 10)
{
   fclose($fp);
   die("unable to aquire a lock");
}

// We get a lock
// Do things
fwrite($fp, "Stuff");

// Release lock :
flock($fp, 3);
fclose($fp);
0
 
gamebossAuthor Commented:
to maxkir:
the "flock()" always return true,so the "while()"part of your code always be run.
0
 
gamebossAuthor Commented:
to maxkir:
the "flock()" always return true,so the "while()"part of your code always be run.
0
 
gamebossAuthor Commented:
to maxkir:
the "flock()" always return true,so the "while()"part of your code always be run.
0
 
maxkirCommented:
First, it will be run only 10 attempts only if flock returns false.

Second, why it will always return true ? It returns true when it will be able to acquire the lock.

Another way - use semaphores:

$sem = 0x12345;
$sem_id = sem_get($sem);

sem_acquire($sem_id);
$fp = fopen("/tmp/aaa", "w");

for($i=0; $i<100000; $i++)
{
fwrite($fp, "Stuff");
}
 fclose($fp);
sem_release($sem_id);


0
 
gamebossAuthor Commented:
to maxkir:
I tryed on my computer(windows95).
whatever the file whether be lock,your code always sleep 10 seconds.

0
 
maxkirCommented:
Well, I see. You can't use semaphores.

And i suggest that flock works only on UNIX/WinNT, but I'm not sure.

Well, then use gravities or us111 comments ...
0
 
gamebossAuthor Commented:
Adjusted points from 10 to 30
0
 
gamebossAuthor Commented:
thank you very much.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now