28, May 2012

file_put_contents with multiple flags - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: file_put_contents with multiple flags
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: file_put_contents with multiple flags  (Read 3806 times)
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« on: Aug 01, 2007, 01:32:48 am »

Hello,

is it possible to use file_put_contents with multiple flags? The PHP documentation says:

... flags can take FILE_USE_INCLUDE_PATH, FILE_APPEND and/or LOCK_EX ...

I need the flags FILE_APPEND and LOCK_EX. It's possible to do the same with fopen(), fwrite() and fclose() but I'm looking for a simple and fast solution.

Thank you in advance for your answers.

Best regards,
Tim

Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Aug 01, 2007, 05:52:52 am »

which php5 version do you have?

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #2 on: Aug 01, 2007, 08:40:12 am »

Are you looking for something like this?

Code:
<?php
$fp 
fopen("file.txt""a+");

if (
flock($fpLOCK_EX)) { 
    
fwrite($fp"Write something here\n");
    
flock($fpLOCK_UN); 
} else
    echo 
"Couldn't lock the file !";
fclose($fp);
?>


Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #3 on: Aug 01, 2007, 10:19:44 am »

which php5 version do you have?
I use PHP v5.2.1 The LOCK_EX constant has been introduced in PHP v5.1.0. So there shouldn't be any problems regarding the currently installed version.

Are you looking for something like this?

Code:
<?php
$fp 
fopen("file.txt""a+");

if (
flock($fpLOCK_EX)) { 
    
fwrite($fp"Write something here\n");
    
flock($fpLOCK_UN); 
} else
    echo 
"Couldn't lock the file !";
fclose($fp);
?>

No, I want to use file_put_contents because of its performance and simpleness. To specify my problem: The documentation of PHP says that it's possible to have more than one flag in the third parameter but I don't know exactly how it works. Maybe I need to do that with binary operators?

file_put_contents($filename, $contents, FILE_APPEND & LOCK_EX);

...or do I need to put the flags into an array?

file_put_contents($filename, $contents, array (FILE_APPEND, LOCK_EX));
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #4 on: Aug 01, 2007, 10:23:10 am »

I am not sure if this will work, but you can try the array thing.

The other one (with the &) it wont work for sure as this is a logical + and it will produce a TRUE value.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Aug 01, 2007, 10:30:52 am »

strange in the manual they say the flag is an interger data type

my offline manual give this example with the OR method:

file_put_contents ('sample.txt', 'some data', FILE_APPEND | LOCK_EX);

so it might be:
file_put_contents ('sample.txt', 'some data', FILE_APPEND & LOCK_EX);

?

Tim just try it Cheesy

Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6968
Tags : php file_put_contents FILE_APPEND LOCK_EX flags Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: file_put_contents with multiple flags
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 10:18:52 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.