Solved

flock function

Posted on 2016-09-02
3
34 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 56

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 110

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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