Solved

flock function

Posted on 2016-09-02
3
14 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 51

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 108

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now