login  Naam:   Wachtwoord: 
Registreer je!
 Overzicht:
Overzicht > PHP > Bestandssysteem > file_put_contents()

Gebruik:
int file_put_contents ( string filename, string data )

Uitleg:

Deze functie schrijft een string naar een bestand.
file_put_contents geeft het aantal bytes terug, dat gescreven is naar het bestand.




Voorbeeld:
<?php
 $bytes 
file_put_contents("map/naar/bestand.txt""Hello World!")
?>

Bijdragen :
Stijn
file_put_contents zal de oude data overschrijven met de nieuwe. Als je de nieuwe data na de oude data wilt schrijven zal je volgende functie moeten gebruiken.

  1. /**
  2.  * append data to a file
  3.  *
  4.  * @param string $filename name of the file
  5.  * @param string $contents data to append
  6.  * @param boolean $append_after if this is true it appends the contents after the old. If false it appends it before the old contents.
  7.  * @return int bytes
  8.  */
  9. function file_append_contents( $filename , $contents , $append_after = true )
  10. {
  11.  
  12. $old_contents = file_get_contents( $filename );
  13. $new_contents = ($append_after ) ? $old_contents . $contents : $contents . $old_contents;
  14.  
  15. return file_put_contents( $filename , $new_contents );
  16.  
  17. }
Wim
PHP4 versie:

  1. <?php
  2. if(function_exists('file_put_contents') === false)
  3. {
  4. function file_put_contents ($file, $contents) // writen by Wim Mariën. Untested!
  5. {
  6. if(!is_file($file))
  7. return false;
  8.  
  9. $sizeAtStart = filesize ($file, 'a');
  10. $fhandle = fopen ($file);
  11. fwrite ($fhandle, $contents);
  12. fclose ($fhandle);
  13. $sizeAfterWriting = filesize($file);
  14.  
  15. return ($sizeAfterWriting - $sizeAtStart);
  16. }
  17. }
  18. ?>
svm
Helaas is deze functie alleen voor PHP5.
Als je een andere versie hebt, dan zul je gebruik moeten maken van fwrite().

Of gebruik bovenstaande functie (ook met fwrite() maar wel zo handig).


 Overzicht
chmod()
copy()
fclose()
filemtime()
filesize()
file_exists()
file_get_contents()
file_put_contents()
fopen()
fread()
fwrite()
is_dir()
is_file()
is_readable()
is_writable()
move_uploaded_file()
rename()
unlink()



© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.028s