PHP File Manipulation File locking Tutorial
Posted on: April 14, 2006 at 12:00 AM
Why should you lock the file every time when you write or read data? Cause two instances of your script could be writing data to the same file in the same time.

PHP File locking

Learn how to use PHP file locking.

You need to lock the file each time when you do any input/output operation with that file, for example, guest book or forum or counter script.
It seems to be all right, when you simple open file and read/write data from it like this.

$fp=fopen("text",'a+');
    fwrite($fp, "$REMOTE_ADDR\n\r");
fclose($fp)


But if you use this code in any web script (for guestbook or forum or something), there is probability for this script to crash with errors or even to crash deleting all the data previously stored in the file. Because you can't avoid the situation when two users simultaneously add and retreive data from the file, for example getting and adding data to a guestbook in one time. To avoid this, you should use file locking procedure. The existance and internal structure of this procedure depends on operation system where you use PHP.

bool flock (int fp, int operation [, int wouldblock])
where
  fp: file handle
  operation can be:
     LOCK_SH - block for reading (1)
     LOCK_EX - block for writing (2)
     LOCK_UN - unblock (3)
  wouldblock: non necessary field (set LOCK_NB, to not block while locking)

Using this function you can secure your application from crashes when it is executing input/output operation. Modified code that reads data from a file looks like this:

$filename="text.txt";
$fp=fopen($filename,'r');
  flock($fp, LOCK_SH);
    $contents = fread($fp, filesize($filename));
  flock($fp, LOCK_UN);
fclose($fp);


To write data into the file using lock write something like this:

$filename="text.txt";
$fp=fopen($filename,'r');
  flock($fp, LOCK_EX);
    fwrite($fp, "$REMOTE_ADDR\n\r");
  flock($fp, LOCK_UN);
fclose($fp);


There is also one feature in using file locking function. You should lock/unlock file in any script that deals with that file. So if you have for example one script adding data to a file and another script showing file contents, if you omit file locking in any of the two your locking operations will be useless. So it is highly recommended to use file locking anytime you execute input / output operation in your web application.
More Tutorials on roseindia.net for the topic PHP File Manipulation File locking Tutorial.
PHP File Manipulation File locking Tutorial
PHP File locking Learn how to use PHP file locking. You need to lock... to a guestbook in one time. To avoid this, you should use file locking procedure...); There is also one feature in using file locking function. You should lock
 
PHP File Manipulation Writing to a File Tutorial
File Manipulation in PHP       In this tutorial we will learn how to write data to a flat file database...: http://roseindia.net/tutorial/php/phpbasics/tutorial/PHP-File-Handling.html
 
PHP File Manipulation File existance checking Tutorial, PHP check file exists
File existence checking If you want to check whether a file exists, you should use file exists PHP function. Syntax: file_exists($path), where...=/images/file1.jpg; file_exists($DOCUMENT_ROOT.$TmpName); where $DOCUMENT_ROOT - PHP
 
Sitemap PHP Tutorial
PHP | PHP Cookies | File Manipulation in PHP | PHP displaying the time... Questions | Site Map | Business Software Services India PHP Tutorial... Tutorial Section Why PHP Is So Useful | Evolution of PHP for Scripting Needs
 
The file function example in PHP, file php, file in php
In this php tutorial section we will be learning the file() function of PHP. It is used to read the full content of file into an array. Syntax of file...() in PHP <?phpADS_TO_REPLACE_4 $ar1=file("c:/rose1/ram.txt"
 
PHP duplicate file - PHP
PHP duplicate file   i wanted to duplicate a PHP file resides in different directory on submission
 
PHP File Handling
PHP File Handling: Every language supports File handling, file handling has... file handling. PHP supports this feature using the following functions: i) ... word is:1ADS_TO_REPLACE_9 Example 2(w-mode): <?php $file=fopen("
 
PHP function is_uploaded_file() and example
Syntax for PHP is_uploaded_file() Function bool is_uploaded_file (file_name... POST protoocol or not. Example of PHP is_uploaded_file() Function Code of PHP is_uploaded_file() Function <?php     $b1
 
PHP function is_file() and example
bool_is_file function states whether the given file is regular file or not Syntax bool is_file(file_name) ADS_TO_REPLACE_1 Example of PHP is_file() PHP Code <?php     $a=is_file("bb.txt"
 
PHP Files management functions and examples.
The PHP file management is import in any application development... to read the some data from the file and display on the users browser. PHP scripting language provides many functions for the manipulation of files from the PHP
 
php download file script
php download file script  PHP script to download file in IE
 
php file search
php file search  How to search file in PHP .. $dir = '/tmp
 
PHP File based guestbook, PHP Guestbook
File based guestbook In this tutorial we will create a file based PHP guestbook in this tutorial. I have decided to put everything into one executable file... to use file locking functions if you want stable guestbook. $fp=fopen
 
file_exists php not working
file_exists php not working  file_exists php not working. Please give me the simple example
 
PHP hide file path
PHP hide file path  PHP to read a path and convert that to the virtual link
 
php parse ini file
php parse ini file  I need a simple and easy example to Parsing an ?advanced? INI file with PHP
 
php download file code
php download file code  PHP code to download the files from the remote server
 
change file permission in php
change file permission in php  How to change file permission in PHP
 
php search file
php search file   I wanted to write a php script to list all the files available in either folder or sub folder
 
PHP error uploading file - PHP
PHP error uploading file  I am getting error while uploading a file in a folder in PHP ... Warning: Cannot modify header information - headers already send any idea
 
Multiple File Upload in PHP
Multiple File Upload in PHP  Hi, I am beginner in PHP scripting language. I am very interested to learn PHP application. So, can anyone explain or provide related reference about how to Multiple file upload in PHP. Thanks
 
php download file browser
php download file browser  limiting downloading file system via browser only through my web application
 
Read external file in PHP
Read external file in PHP  How to read external files in PHP in particular time duration and save that file with the latest date. How is it possible? Please explain with an example. Thanks
 
PHP set_file_buffer() function with example
Syntax for PHP set_file_buffer() Function set_file_buffer(file_handle,buffer..._TO_REPLACE_1 PHP set_file_buffer() Function with Example Code for PHP set_file_buffer() Function <?php     $file=fopen("bb.txt","w
 
PHP function uploaded_file() and example
Syntax for PHP uploaded_file() Function bool move_uploaded_file (file_name... only to the files uploaded by HTTP POST PHP uploaded_file() Function with Example Code for PHP uploaded_file() Function <html> <body>
 
Import MYSQL file in PHP - PHP
Import MYSQL file in PHP  i wanted to import the mysql file in PHP5 on button click. is it possible to do it? Is there any possibility of data security
 
Detecting the file extension
Detecting the file extension   How to detect the file extension in PHP?   Read the given tutorial to detect the file extension in PHP. The fileexists() function, fileexists php, file_exists in php
 
php download file from ftp
php download file from ftp  Need to download files from ftp using php. a simple example
 
php download file from url
php download file from url  how to download multiple files in PHP using the URL's
 
PHP Getting Started With PHP Tutorial
; Learn the basics of PHP with this quick and simple tutorial. Designed...://roseindia.net/tutorial/php/phpbasics/tutorial/index.html orADS_TO_REPLACE_11 http://roseindia.net/tutorial/php/phpbasics/index.html 
 
Manipulation of war fie contents
Manipulation of war fie contents  How can I manipulate a specific file inside my war file without extracting it? Please help... Thanks and regards, Swapnil
 
PHP Training
, and authication the user with session control), Emailing in PHP and File System Management in PHP covering from Opening a File in PHP to File Locking using... of fwrite() function in PHP Writing to a File in PHP File Locking
 
zip file upload in php - WebSevices
zip file upload in php  This is not a normal upload. we know the code for normal upload. i need the zip file upload code in php. how to upload zipfile using php? and how to unzip using php? please i dont
 
PHP file_get_contents() Function
PHP file_get_contents() Function  Hi, How do i read the content of file using PHP program? So kindly provide any example or online help reference for filegetcontents() Function in PHP. Please feel free to suggest. Thanks
 
Check the File Exists in PHP Program
Check the File Exists in PHP Program  Hi, I trying to create a application to find or retrieve the existing files. So, Please help me or suggest any online reference that explain about the file exists function in PHP? Thanks
 
string manipulation in xml
string manipulation in xml  im working on build.xml using apache ant... in one of the targets i am assigning a var file with the full path of .sql files present in a folder. while executing that .sql file in xml its giving me
 
PHP Open Excel File in browser - PHP
PHP Open Excel File in browser  How we can open an excel file using PHP code. I don?t want to let the user to download it rather I would like to get it open in the web browser. Any help would be great. Thank you very much
 
PHP function parse_ini_file() and example
Syntax of PHP parse_ini_file() Function It is used to parse ini file.ini.... of the ini file in array form PHP parse_ini_file() Function with Example Code For PHP parse_ini_file() FunctionADS_TO_REPLACE_1 make a sample.ini
 
resize the tiff file in php - WebSevices
resize the tiff file in php  please guide me how i resize the tiff file and i want to upload it? using php  Hi I am sending the code of file upload in php. please check it, i hope it will help you... FileUpload.php
 
The file_exists() function, file_exists php, file_exists in php
The file_exists() function returns TRUE if the file or directory specified by filename exists; FALSE otherwise.  Syntax of file_exists() Function PHPADS_TO_REPLACE_1 bool file_exists (file_name ) It test the existence
 
PHP is_executable() Function and example
Syntax bool is_executable(file_name) Find the PHP is_executable() function states whether the given file is executable or not ADS_TO_REPLACE_1 Example of PHP is_executable Fu Code <?php    
 
PHP PHP Triad Tutorial
PHP-Triad Tutorial       In this PHPTriad tutorial we will learn how to download install and test an application on the PHPTriad server. PHPTriad is a software package which installs PHP
 
PHP function ftruncate and example
Syntax int ftruncate(file_handle,length) ftruncate function truncates the file content with the given length. And it returns the truncated file size. PHP ftruncate() Function Example CodeADS_TO_REPLACE_1 <?php $file1
 
tutorial for file upload in spring - Spring
tutorial for file upload in spring  Is there tutorial available for uploading file using spring framework. The example in the spring reference uses... : FileUploadController.java file package example; import
 
php
php  how to write a program in php to create pdf file
 
how to create a php script to download file
how to create a php script to download file  php script to download file
 
Maven dependency for one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library version 2021-12-13_00-16_4d2b3f6 is released. Learn to use Generic-Locking-Library version 2021-12-13_00-16_4d2b3f6 in Maven based Java projects
; - Version 2021-12-13_00-16_4d2b3f6 of Generic-Locking-Library released... - Generic-Locking-Library project have released the latest version of this library...; one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library library is 2021-12-13_00-16_4d2b3f6
 
Maven dependency for one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library version 2021-12-13_16-51_8bd4142 is released. Learn to use Generic-Locking-Library version 2021-12-13_16-51_8bd4142 in Maven based Java projects
; - Version 2021-12-13_16-51_8bd4142 of Generic-Locking-Library released... - Generic-Locking-Library project have released the latest version of this library...; one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library library is 2021-12-13_16-51_8bd4142
 
Maven dependency for one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library version 2021-12-16_21-59_c896f61 is released. Learn to use Generic-Locking-Library version 2021-12-16_21-59_c896f61 in Maven based Java projects
; - Version 2021-12-16_21-59_c896f61 of Generic-Locking-Library released... - Generic-Locking-Library project have released the latest version of this library...; one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library library is 2021-12-16_21-59_c896f61
 
Maven dependency for one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library version 2021-12-13_22-50_bba1419 is released. Learn to use Generic-Locking-Library version 2021-12-13_22-50_bba1419 in Maven based Java projects
; - Version 2021-12-13_22-50_bba1419 of Generic-Locking-Library released... - Generic-Locking-Library project have released the latest version of this library...; one.jkr.de.jkrsoftware.entity.locking.libraries - Generic-Locking-Library library is 2021-12-13_22-50_bba1419
 
Ads

Related Tags for PHP File Manipulation File locking Tutorial:


Ads

 
Advertisement null

Ads