communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "file handling" - download "write to first line of file"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

file handling - write to first line of file was created/last modified on Mon 31 May 2004 9:11 AM

this snippet let's you insert data at the beginning of a file.
it reads the contents and adds new data to the first line of the file.
after this it joins the lines and writes them back to the file again.

<?php

// your new data + newline
$new_line = 'some new data here'."\n";

// the filepath
$file = 'temp/file.txt';
// the old data as array
$old_lines = file($file);
// add new line to beginning of array
array_unshift($old_lines,$new_line);
// make string out of array
$new_content = join('',$old_lines);
$fp = fopen($file,'w');
// write string to file
$write = fwrite($fp, $new_content);
fclose($fp);

?>

learn more about some of the PHP functions used in this snippet: array, array_unshift, fclose, file, fopen, fwrite, join

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari