PHP file_put_contents() Function

In this chapter you will learn:

  1. Definition for PHP file_put_contents() Function
  2. Syntax for PHP file_put_contents() Function
  3. Parameter for PHP file_put_contents() Function
  4. Return for PHP file_put_contents() Function
  5. Note for PHP file_put_contents() Function
  6. Example - Write to a file

Definition

The file_put_contents() writes a string to a file.

Syntax

PHP file_put_contents() Function has the following syntax.

file_put_contents(file,data,mode,context)

Parameter

ParameterIs RequiredDescription
fileRequired.File to write. For non-existing file, this function will create one
dataRequired.Data to write. Can be a string, an array or a data stream
modeOptional.How to open/write to the file.
contextOptional.Context of the file handle.

Possible values for mode:

  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX

Return

This function returns the number of bytes that were written to the file, or FALSE on failure.

Note

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.

Use the === operator for testing the return value of this function.

Example

file_put_contents() and fwrite() complement functions file_get_contents() and fread(), respectively.


<?PHP//from  j  av  a  2  s . c om
      echo file_put_contents("test.txt","Hello World. Testing!");


      $myarray[] = "This is line one";
      $myarray[] = "This is line two";
      $myarray[] = "java2s.com";
      $mystring = implode("\n", $myarray);
      $filename = "test.txt";
      $numbytes = file_put_contents($filename, $mystring);
      print "$numbytes bytes written\n";
?>

The third parameter to file_put_contents() can be set to FILE_APPEND, If you do not use FILE_APPEND, the existing text will be replaced.

Use FILE_APPEND to avoid deleting the existing content of the file.

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP fileatime() Function
  2. Syntax for PHP fileatime() Function
  3. Parameter for PHP fileatime() Function
  4. Note for PHP fileatime() Function
  5. Return for Is Required
  6. Example - Get last accessed time.
Home » PHP Tutorial » PHP File Functions
PHP basename() function
PHP chdir() function
PHP chgrp() Function
PHP chmod() Function
PHP chown() Function
PHP chroot() Function
PHP clearstatcache() Function
PHP closedir() Function
PHP copy() Function
PHP delete() function
PHP dir() Function
PHP dirname() Function
PHP disk_free_space() Function
PHP disk_total_space() Function
PHP diskfreespace() Function
PHP fclose() Function
PHP feof() Function
PHP fflush() Function
PHP fgetc() Function
PHP fgets() Function
PHP fgetss() Function
PHP file() Function
PHP file_exists() Function
PHP file_get_contents() Function
PHP file_put_contents() Function
PHP fileatime() Function
PHP filectime() Function
PHP filegroup() Function
PHP fileinode() Function
PHP filemtime() Function
PHP fileowner() Function
PHP fileperms() Function
PHP filesize() Function
PHP filetype() Function
PHP flock() Function
PHP fnmatch() Function
PHP fopen() Function
PHP fpassthru() Function
PHP fprintf() Function
PHP fputcsv() Function
PHP fputs() Function
PHP fread() Function
PHP fscanf() Function
PHP fseek() Function
PHP fstat() Function
PHP ftell() Function
PHP ftruncate() Function
PHP fwrite() Function
PHP getcwd() function
PHP glob() Function
PHP is_dir() Function
PHP is_executable() Function
PHP is_file() Function
PHP is_link() Function
PHP is_readable() Function
PHP is_uploaded_file() Function
PHP is_writable() Function
PHP link() Function
PHP linkinfo() Function
PHP lstat() Function
PHP md5_file() Function
PHP mkdir() Function
PHP move_uploaded_file() Function
PHP opendir() Function
PHP parse_ini_file() Function
PHP pathinfo() Function
PHP pclose() Function
PHP popen() Function
PHP readdir() Function
PHP readfile() Function
PHP readlink() Function
PHP realpath() Function
PHP rename() Function
PHP rewind() Function
PHP rewinddir() Function
PHP rmdir() Function
PHP scandir() Function
PHP set_file_buffer() Function
PHP sha1_file() Function
PHP stat() Function
PHP symlink() Function
PHP tempnam() Function
PHP tmpfile() Function
PHP touch() Function
PHP umask() Function
PHP unlink() Function
PHP vfprintf() Function