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.

    fwrite($fp, "$REMOTE_ADDR\n\r");

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])
  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:

  flock($fp, LOCK_SH);
    $contents = fread($fp, filesize($filename));
  flock($fp, LOCK_UN);

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

  flock($fp, LOCK_EX);
    fwrite($fp, "$REMOTE_ADDR\n\r");
  flock($fp, LOCK_UN);

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 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.../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... by PHP  | PHP Ajax | PHP SimpleXML | PHP Ajax DB PHP Tutorial Section
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...() function in PHP array file ( filename, [,flags [, context]] ) It returns...; the context  Code for file() in PHP <?php $ar1=file("c
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) ...): <?php $file=fopen("PHP.txt","r")or exit("
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) Example of PHP is_file() PHP Code <?php     $a=is_file("bb.txt");    
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 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
change file permission in php
change file permission in php  How to change file permission in PHP
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
file_exists php not working
file_exists php not working  file_exists php not working. Please give me the simple example
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 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
php download file browser
php download file browser  limiting downloading file system via browser only through my web application
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
PHP set_file_buffer() function with example
Syntax for PHP set_file_buffer() Function set_file_buffer(file_handle,buffer) It sets the buffer of the given file_handler with the given size PHP set_file_buffer() Function with Example Code for PHP set_file_buffer() Function
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
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
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 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
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 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
The file_exists() function, file_exists php, file_exists in php
PHP bool file_exists (file_name ) It test the existence of the file...() Function PHP  <?php echo "file is existing ".file... The file_exists() function returns TRUE if the file or directory
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
PHP Getting Started       Learn the basics of PHP with this quick and simple tutorial. Designed...:// or
PHP Training
from Opening a File in PHP to File Locking using flock() function in PHP. We...() function in PHP Writing to a File in PHP File Locking using flock..., with a particular study of MySQL Here is the video tutorial of "PHP
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
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
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() Function make a sample.ini with the content 
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
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 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 Code <?php $file1=fopen("bb.txt","r
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 Example of PHP is_executable Fu Code <?php     if (is_executable("c
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... : file package example; import
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
Double-checked locking,java tutorials,java tutorial
Double-checked Locking Lets us have a look of Singleton pattern, In Singleton pattern we used some type of synchronization in java, so that only only one... prefer Double-Checked locking. In double checked locking we do something like
php  how to write a program in php to create pdf file
File Upload
File Upload  Hi I need script / string in PHP where I uploaded the file in html frontend and it should sent the uploaded file as attachment to my email address
php gd Wbmp
<?php $im = imagecreatetruecolor(120, 20); $text_color..._color); imagewbmp($im, 'simpletext.wbmp'); echo "Wbmp file created and saved successfully"; imagedestroy($im); ?> output : Wbmp file
how to create a php script to download file
how to create a php script to download file  php script to download file
File Upload Tutorial With Examples In JSP
File Upload Tutorial With Examples In JSP       This tutorial is very helpful... by Using JSP This tutorial will help you to understand how you can upload a file

Related Tags for PHP File Manipulation File locking Tutorial: