Write a file Using Php
In PHP, there are many functions available which is used to write a file.
The functions are...
fwrite(), fputs(), file_put_contents
Syntax:
fputs-
fputs($file,$string,$length);
fwrite-
fwrite($file,$string,$length);
The fwrite() or fputs use to writes to an open file.This function returns the number of bytes written on success and FALSE on failure.
fwrite() and fputs functions are binary-safe i.e both binary data, like images, and character data can be written with this function.
Args:
$file- Required. Specifies the open file to write to.
$string- Required. Specifies the string to write to the open file.
$length- Optional. Specifies the maximum number of bytes to write
Note: There is no any difference between fputs() and fwrite() functions.
The fwrite() function is an alias of the fputs() function.
Example:
<?php
/*
* Created By: Evon Tech
* Date:4/5/2015
*/
error_reporting(E_ALL);
ini_set('display_errors', '1');
$file = fopen("writetestfile.txt","w");
//echo fwrite($file,"Hello World. Testing!");
//It gives total number of character which is written in file on success
if(fwrite($file,"Hello, Evon Tech provides the best IT service"))
{
echo "file write operation is successful";
}else{
echo "There is some error occured, it may be file file permission";
}
fclose($file);
fclose($file);
?>
file_put_contents(): This function is used to writes a string to a file.
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.
Syntax:
file_put_contents($file,$data,$mode,$context);
Execution of file_put_contents():
If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filename
1. Create the file if it does not exist
2. Open the file
3. Lock the file if LOCK_EX is set
4. If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content
5. Write the data into the file
6. Close the file and release any locks
This function returns the number of character written into the file on success, or FALSE on failure.
Args:
$file- Required. Specifies the open file to write to.If the file does not exist, this function will create one
$data- Required. The data to write to the file. Can be a string, an array or a data stream.
$mode- FILE_USE_INCLUDE_PATH ,FILE_APPEND ,LOCK_EX
$context- Optional. Specifies the context of the file handle. Context is a set of options that can modify the behaviour of a stream.
Note: Use FILE_APPEND to avoid deleting the existing content of the file
Example:
<?php
/*
* Created By: Evon Tech
* Date:4/5/2015
*/
error_reporting(E_ALL);
ini_set('display_errors', '1');
$targetFilename = "writeinfile.odt";
if(file_put_contents($targetFilename,"Hello, Evon Tech provides the best IT service",FILE_APPEND))
{
echo "file write operation is successful";
}else{
echo "There is some error occurred, it may be file file permission";
}
?>
Note: If you get error like 'Warning: file_put_contents(writeinfile.odt): failed to open stream: Permission denied in /var/www/filehandling/writefile.php on line 10' , check your file permission.
0 Comment(s)