Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-04-2009
Member
 
Join Date: Apr 2009
Posts: 64
Best way to set Lock on PHP File

Hello,

Does anyone know the best way to lock a PHP file after it has been executed. I created a script which is called setup.php which will execute and once it has been executed cannot run again until an unless we remove the lock set on that file,So can anyone suggest me the Best possible way to set Lock on PHP file.

Thanks for your valuable support.
Reply With Quote
  #2  
Old 14-04-2009
Member
 
Join Date: Dec 2008
Posts: 183
Re: Best way to set Lock on PHP File

One simple way would be to deny access to "install.txt" via htaccess or another access manager, then you can just toggle the file extension from php to txt and vice-versa.
Reply With Quote
  #3  
Old 14-04-2009
Member
 
Join Date: Apr 2008
Posts: 193
Re: Best way to set Lock on PHP File

You can lock a file use flock() function. This function allows you to performance a simple reader/write model. Once a file is locked with flock(), other processing attempting to write to file have to wait until unlock. It avoid multiprocess at a file that can corrupt it.

Code:
<?php 
2	$f = "test2.txt"; 
3	 
4	$fo = fopen($f, "wb+") or die("cannot open"); 
5	 
6	if(flock($fo, LOCK_EX)){ 
7	  fwrite($fo,"test add a 2 string") or die("cannot write to file"); 
8	  flock($fo, LOCK_UN); 
9	}else{ 
10	  die("Cannot lock file"); 
11	} 
12	 
13	fclose($fo); 
14	echo "sucess"; 
15	?>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Best way to set Lock on PHP File"
Thread Thread Starter Forum Replies Last Post
Using folder lock leading to loss of file Barraq Operating Systems 3 02-04-2011 03:10 AM
Unable to lock pdf file in Adobe Reader Halyn Windows Software 5 07-04-2010 12:30 PM
Lock icon appears beside file names on windows 7 AzzamKhan Operating Systems 4 07-04-2010 03:12 AM
Windows 7 lock down the PC during the file copying Carol.fire Operating Systems 3 08-12-2009 05:02 PM
Usb file transfer lock up PATIENCE Hardware Peripherals 4 24-08-2009 11:04 AM


All times are GMT +5.5. The time now is 10:37 PM.