Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

flock function

Posted on 2016-09-02
3
Medium Priority
?
42 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

604 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