Results 1 to 3 of 3

Thread: Best way to set Lock on PHP File

  1. #1
    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.

  2. #2
    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.

  3. #3
    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	?>

Similar Threads

  1. Using folder lock leading to loss of file
    By Barraq in forum Operating Systems
    Replies: 3
    Last Post: 02-04-2011, 03:10 AM
  2. Unable to lock pdf file in Adobe Reader
    By Halyn in forum Windows Software
    Replies: 5
    Last Post: 07-04-2010, 12:30 PM
  3. Lock icon appears beside file names on windows 7
    By AzzamKhan in forum Operating Systems
    Replies: 4
    Last Post: 07-04-2010, 03:12 AM
  4. Windows 7 lock down the PC during the file copying
    By Carol.fire in forum Operating Systems
    Replies: 3
    Last Post: 08-12-2009, 05:02 PM
  5. Usb file transfer lock up
    By PATIENCE in forum Hardware Peripherals
    Replies: 4
    Last Post: 24-08-2009, 11:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,486,493.11904 seconds with 16 queries