Jump to: navigation, search

Php file put contents

From w3cyberlearnings

Contents

PHP function file_put_contents

This function writes a string to a file.

Syntax file_put_contents

  • file: file name
  • data: string or array stream resource.
  • mode: Can be combined with binary OR operator
    • FILE_USE_INCLUDE_PATH: Search file in the include_path
    • FILE_APPEND: Append to a file if already existed.
    • LOCK_EX: Acquire an exclusive lock on the file while processing to the wrting.
  • context:A valid context resource created with stream_context_create().
file_put_contents(file, data, mode, context);

File Content (mycontent.txt)

A peaceful school makes a peaceful student.

Example 1


<?php

$content = file_get_contents("mycontent.txt");
$content .="\nA Peaceful world with a a peaceful person!\n";
file_put_contents("mycontent.txt", $content);
?>

Output (mycontent.txt)

A peaceful school makes a peaceful student.
A Peaceful world with a a peaceful person!

Example 2

<?php 
  file_put_contents("mycontent.txt","\nGood people.\n", FILE_APPEND|LOCK_EX);
?>

Related Links


basename-- chgrp-- chmod-- chown-- clearstatcache-- copy-- delete-- dirname-- disk_free_space-- disk_total_space-- diskfreespace-- fclose-- feof-- fflush-- fgetc-- fgetcsv-- fgets-- fgetss-- file_exists-- file_get_contents-- file_put_contents- file-- fileatime-- filectime-- filegroup-- fileinode-- filemtime-- fileowner-- fileperms-- filesize-- filetype-- flock-- fnmatch-- fopen-- fpassthru-- fputcsv-- fputs-- fread-- fscanf-- fseek-- fstat-- ftell-- ftruncate-- fwrite-- glob-- is_dir-- is_executable-- is_file-- is_link-- is_readable-- is_uploaded_file-- is_writable-- is_writeable-- lchgrp-- lchown-- link-- linkinfo-- lstat-- mkdir-- move_uploaded_file-- parse_ini_file-- parse_ini_string-- pathinfo-- pclose-- popen-- readfile-- readlink-- realpath_cache_get-- realpath_cache_size-- realpath-- rename-- rewind-- rmdir-- set_file_buffer-- stat-- symlink-- tempnam-- tmpfile-- touch-- umask-- unlink--

Navigation
Web
SQL
MISC
References