Solved

flock function

Posted on 2016-09-02
3
37 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 57

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

718 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