Introduction
In this article I describe the PHP FileSystem functions fopen, fpassthru, fputcsv and fputs. To learn some other FileSystem functions, go to:
- FileSystem Function in PHP: PART 1
- FileSystem Function in PHP: PART 2
- FileSystem Function in PHP: PART 3
- FileSystem Function in PHP: PART 4
- FileSystem Function in PHP: PART 5
- FileSystem Function in PHP: PART 6
- FileSystem Function in PHP: PART 7
- FileSystem Function in PHP: PART 8
PHP fopen() Function
The PHP fopen function is used to opens files or an URL and it returns a file pointer resource on success or false on failure.
Syntax
| fileopen(filename,mode,include_path,context) |
Parameters of the fopen function
The parameters of the function are:
| Parameter | Description |
| filename | It specifies the file or URL to open. |
| mode | It specifies the type of access you require for the file and the possible values are:
|
| include_path | Set the parameter to 1 if you want to search for the file in the include_path. |
| context | It specifies the context of the file handle. |
Example
An example of the function is:
<?php
$file = fopen("test.txt","r");
$file = fopen("/wamp/www/test.txt","r");
?>
PHP fpassthru() Function
The PHP fpassthru function is used to read all data from the current position in an open file until end-of-file (EOF) and writes the result to the output buffer.
Syntax
| fpassthru(file) |
Parameter in the fpassthru function
The parameter of the function is:
| Parameter | Description |
| file | It specifies the open file to resource to read from. |
Example
An example of the function is:
<?php
$file = fopen("test.txt","r");
fgets($file);
echo fpassthru($file);
fclose($file);
?>
Output
PHP fputcsv() Function
The PHP fputcsv function formats a line as CSV and writes it to an open file and returns the length of the string written or FALSE on failure.
Syntax
| fputcsv(file,fields,seperator,enclouser) |
Parameter in fputcsv function
The parameters of the function are:
| Parameter | Description |
| file | It specifies the open file to write to. |
| fields | It specifies which array to get the data from. |
| seperator | It specifies a character that specifies the field separator. |
| enclouser | It specifies a character that specifies the field enclosure character. |
Example
An example of the function is:
<?php
$file1=fopen("test.csv","r");
echo "values before fputcsv()<br>";
while(($array1=fgetcsv($file1))!==FALSE)
foreach ($array1 as $key=>$value)
echo " ".$value;
?>
Output
PHP fputs() Function
The PHP fputs function writes to an open file and the function fputs is the alias of the fwrite function.
Syntax
| fputs(file,string,length) |
Parameter in fputs function
The parameters of the function are:
| Parameter | Description |
| file | It specifies the open file to write to. |
| string | It specifies the string to write to the open file. |
| length | It specifies the maximum number of bytes to write. |
Example
An example of the function is:
<?php
$file = fopen("test.txt","w");
echo fputs($file,"Again Welcome at MCN Solution!");
fclose($file);
?>
Output

Join the conversation! Your thoughts help the community grow.