Lampwrights Forum > Linux, Apache, MySQL, PHP > PHP
Register Community Today's Posts Search

Reply
 
Thread Tools
02-01-2011, 08:22 AM   #1
Jeff
Administrator
 
Jeff's Avatar
 
Join Date: Jul 2010
Posts: 402
Rep Power: 10
Jeff is getting browny points
PHP Writing Text To A File

There are many times you need to write text to a file. Unfortunately when you search google you usually find examples are that use fwrite, fopen, etc.. They are fine grained sure, but most of the time you only need quick and dirty file writing. There is a built in PHP function that lets you write to a file in just one line:

Code:
file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
The $file variable is the complete path and file name to the file you want to write to, example: /foo/bar/file.txt. The $data variable is what you want written. The last section is optional. It tells PHP to append to the file rather than overwrite which it does by default. LOCK_EX just exclusively locks the file.

Enjoy! Less code is more!
Jeff is offline   Reply With Quote

Reply

Tags

other php

,

php

,

php examples

,

php how-to



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My favorite text editor for PHP Jeff PHP 3 06-22-2011 07:04 AM


All times are GMT -4. The time now is 01:40 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2024, vBulletin Solutions, Inc.