FileSystem Function in PHP: PART 9

Introduction

In this article I describe the PHP FileSystem functions fopen, fpassthru, fputcsv and fputs. To learn some other FileSystem functions, go to:

  1. FileSystem Function in PHP: PART 1
  2. FileSystem Function in PHP: PART 2
  3. FileSystem Function in PHP: PART 3
  4. FileSystem Function in PHP: PART 4
  5. FileSystem Function in PHP: PART 5
  6. FileSystem Function in PHP: PART 6
  7. FileSystem Function in PHP: PART 7
  8. 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:
  • "r": It is read only and starts at the beginning of the file.
  • "r+": It is read write and starts at the beginning of the file.
  • "w" -It is write only and opens and clears the contents of file.
  • "w+": It is read or write and opens and clears the contents of file.
  • "a" -It is write only and opens and writes to the end of the file or creates a new file if it doesn't exist.
  • "a+": It is read or write and preserves file content by writing to the end of the file.
  • x": It is write only and creates a new file and returns FALSE and an error if file already exists.
  • "x+": It is read or Write and creates a new file and returns FALSE and an error if the file already exists.
     
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

fpassthru-function-in-php.jpg 

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

fputcsv-function-in-php.gif

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

fputs-function-in-php.gif

Up Next
    Ebook Download
    View all
    Learn
    View all