[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

have function to check file whether are opening

Posted on 2000-03-22
16
Medium Priority
?
203 Views
Last Modified: 2006-11-17
have function to check a file whether are open?
0
Comment
Question by:gameboss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +2
16 Comments
 
LVL 8

Expert Comment

by:us111
ID: 2648228
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
 
LVL 2

Expert Comment

by:maxkir
ID: 2648349
No.
0
 

Expert Comment

by:mrvithan
ID: 2648566
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:gameboss
ID: 2648908
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
 
LVL 8

Expert Comment

by:us111
ID: 2649135
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
 
LVL 2

Expert Comment

by:gravity
ID: 2649616
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
 
LVL 8

Expert Comment

by:us111
ID: 2649712
gravity has right too
0
 
LVL 2

Expert Comment

by:maxkir
ID: 2649819
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
 

Author Comment

by:gameboss
ID: 2652069
to maxkir:
the "flock()" always return true,so the "while()"part of your code always be run.
0
 

Author Comment

by:gameboss
ID: 2652182
to maxkir:
the "flock()" always return true,so the "while()"part of your code always be run.
0
 

Author Comment

by:gameboss
ID: 2652189
to maxkir:
the "flock()" always return true,so the "while()"part of your code always be run.
0
 
LVL 2

Expert Comment

by:maxkir
ID: 2652330
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
 

Author Comment

by:gameboss
ID: 2653074
to maxkir:
I tryed on my computer(windows95).
whatever the file whether be lock,your code always sleep 10 seconds.

0
 
LVL 2

Accepted Solution

by:
maxkir earned 120 total points
ID: 2653231
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
 

Author Comment

by:gameboss
ID: 2655661
Adjusted points from 10 to 30
0
 

Author Comment

by:gameboss
ID: 2655662
thank you very much.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question