Solved

flock function

Posted on 2016-09-02
3
24 Views
Last Modified: 2016-10-22
i saw this code in a book and i didn t understand why flock is being used . and when we use it isn t it like this  
(flock($file1, LOCK_EX)) ???

<?php
$idfile=fopen("connex.txt","a");
$date=time();;
flock($idfile,2);
fwrite($idfile,$date);
flock($idfile,3);
fclose($idfile);
//Lecture des données
$idfile=fopen("connex.txt","r");
flock($idfile,1);
0
Comment
Question by:hamza somai
3 Comments
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41781728
LOCK_SH
LOCK_EX
LOCH_UN
Are defines that have values 1,2 and 3 - so while it makes more sense to use LOCK_EX and more readable - it is also possible to use the numbers. Personally I don't see why they did it that way but it is still valid.

As to why - I imagine it is part of an example to demonstrate how file locking works. First create an exclusive lock on the file to append to it, release the lock and then acquire a shared lock on the file to read it.
1
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41781774
I would always use the LOCK_xx notation.  It's easier to read and understand.  You might want to check the date and popularity of that book.  Using a number when you should be using a named constant seems like a code smell to me!
1

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question