PHP Programming Exercises
- Write a php program to compare between things that are not integers
- Write a division table program in PHP using for loop
- Write a program in PHP to print prime numbers between 1 and 100
- Write a php program to print numbers from 10 to 1 using the recursion function
- Write a php program to store the username in cookie and check whether the user have successfully login or not
- Write a php program to convert the given string into an array
- Write a php program to loop over the json data
- Write a program in PHP to remove all html tags except paragraph and italics tags
- Write a program to loop through an associative array using foreach() or with each()
- Write a php program to differentiate between fgets, fgetss and fgetcsv
- There are two deals of an item to buy. The quantities and prices of the item are given below. Write a program in PHP to find the best deal to purchase the item
- Write a php program to set session on successful login
- Write a program in PHP to read from directory
- PHP create image from text and save
- How to get data from XML file in PHP
- PHP Create Word Document from HTML
- How to check whether a year is a leap year or not in PHP
- Fibonacci Series Program in PHP
- How to generate QR Code in PHP
- How does PHP store data in cache?
- How to detect a mobile device using PHP?
- How to send HTML form data to email using PHP?
- How to get location from IP address using PHP?
- How to lock a file using PHP?
- How to import a CSV file into MySQL using PHP
- How to fetch data from database in PHP and display in PDF
- How to insert image in database using PHP
- How to remove last character from string using PHP?
- Write a PHP program to reverse a string without predefined function
- Write a PHP program to calculate percentage of total
- How to sanitize input for MySQL using PHP?
- Write a program to calculate electricity bill in PHP
- How to send email with SMTP in PHP?
- How to Send Text Messages With PHP?
- How to convert stdClass object to Array in PHP?
- How do I import Excel data into MySQL database using PHP?
- How can I post a form without refreshing the page?
- How to sort table columns with PHP and MySQL?
- How to get current directory, filename and code line number in PHP
File locking is an important task. It restricts other users from changing a specific file. It allows only one user at a time to access or modify it. PHP provides the flock() method for portable advisory file locking. This was introduced with PHP version 4.0. This method provides options to mark the file for locking purposes. There are two kinds of locks we can make with flock- shared locks and exclusive locks. If you want to read only lock the file, use the shared lock, and if you want to lock the file for writing purposes, use the exclusive lock.
Syntax of flock()
flock($file_handler, $operation, $block)
$file_handler- It is a file handler pointer created by using fopen().
$operation- The lock operation can contain one of the following constants-
The $block is optional, set to 1 to block other processes while locking. The flock() function returns a boolean value. It returns TRUE if the file lock is retrieved successfully and FALSE otherwise.
flock() Exclusive lock (LOCK_EX) example
In the above code, we have opened a file 'text.txt' and assigned the file handler to $fh. Next, we lock the file using LOCK_EX (exclusive lock) in flock() function and write the $write variable to the file. The fflush() function flushes the output to the file. Finally, we released the locked file using LOCK_UN in the flock() function and closed the file handler. If you are using PHP version >= 5.3, you must manually unlock the file using the fclose() function.
When you execute the above code, it will append the given content at the end of the text file. Make sure to provide the write file path. If any error occurs during script execution, it will return the value provided in '$php_errormsg'.
PHP flock() Shared lock (LOCK_SH) example
As you saw in the above example, we have a locked file for writing. Here you will know how to read a locked file.
PHP flock() Non-blocking locking example
When the file is locked by another user, and you don't want to block while locking, use LOCK_NB as a bitmask to LOCK_SH or LOCK_EX.
need an explanation for this answer? contact us directly to get an explanation for this answer