Category: >> PHP Code >> Write to File example Bookmark and Share

<< lastnext >>

Snippet Name: Write to File example

Description: A quick example of writing to a flat file.

Also see:
» Simple directory viewer / browser
» Read a web page directly in to a s...
» Recursive Directory Browser
» Saving Remote Images With PHP
» Include All Files In A Directory
» More File Type Detection in PHP
» Get Current Page URL
» Get Current Directory Name
» Measure script run time
» Check if a file exists
» Export table to Excel or MS Word f...
» Count lines in file
» Copy File From Server
» Remove All Files An A Given Direct...
» Calculate File/Folder Size In A Di...

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

$filename = 'test.txt'; 
$somecontent = "Add this text to the file\n"; 
// Let's make sure the file exists and is writable first. 
if (is_writable($filename)) { 
    // In our example we're opening $filename in append mode. 
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it. 
    if (!$handle = fopen($filename, 'a')) { 
         print "Cannot open file ($filename)"; 
    // Write $somecontent to our opened file. 
    if (!fwrite($handle, $somecontent)) { 
        print "Cannot write to file ($filename)"; 
    print "Success, wrote ($somecontent) to file ($filename)"; 
} else { 
    print "The file $filename is not writable"; 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© | users online: 8