0% found this document useful (0 votes)
3 views18 pages

2.5 Filesystem Functions

The document provides an overview of PHP file system functions, detailing how to open, read, write, copy, and delete files using various functions such as fopen(), fread(), fwrite(), and unlink(). It explains the syntax and usage of these functions, including file modes and examples for practical implementation. Additionally, it lists other useful file functions like filetype() and is_dir().

Uploaded by

talinoraind
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

2.5 Filesystem Functions

The document provides an overview of PHP file system functions, detailing how to open, read, write, copy, and delete files using various functions such as fopen(), fread(), fwrite(), and unlink(). It explains the syntax and usage of these functions, including file modes and examples for practical implementation. Additionally, it lists other useful file functions like filetype() and is_dir().

Uploaded by

talinoraind
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PHP File System Functions:

• A file is simply a resource for storing information on a computer.

• Files are usually used to store information such as; configuration settings of a program, simple
data such as contact names against the phone numbers and images, pictures, photos, etc.

• PHP file functions support a wide range of file formats that include; File.txt, File.log, File.csv,
File.gif, file.jpg etc.

• The Filesystem function is used to access and manipulate filesystem. For accessing the files on
the system, the file path will be used.

• Syntax:

file_type function()
PHP File System Functions:
OPENING AND CLOSING FILES

• fopen():

The PHP fopen() function is used to open a file. It requires two arguments stating first the file name
and then mode in which to operate. If an attempt to open a file fails then fopen returns a value of
false otherwise it returns a file pointer which is used for further reading or writing to that file.

Syntax:

fopen($file_name,$mode);

Here, “fopen” is the PHP open file function

“$file_name” is the name of the file to be opened

“$mode” is the mode in which the file should be opened


PHP File System Functions:
• Files modes can be specified as one of the six options in this table.
r
Opens the file for reading only.
Places the file pointer at the beginning of the file.
r+
Opens the file for reading and writing.
Places the file pointer at the beginning of the file.
w
Opens the file for writing only.
Places the file pointer at the beginning of the file and truncates the file to zero length. If files does
not exist then it attempts to create a file.
PHP File System Functions:
w+

Opens the file for reading and writing only. Places the file pointer at the beginning of the file and
truncates the file to zero length. If files does not exist then it attempts to create a file.

Opens the file for writing only.

Places the file pointer at the end of the file. If files does not exist then it attempts to create a file.

a+

Opens the file for reading and writing only. Places the file pointer at the end of the file.

If files does not exist then it attempts to create a file.


PHP File System Functions:
Example of fopen():

<?php

$fileName = "/doc/myFile.txt";

$fp = fopen($fileName,"r");

if( $fp == false )

echo ( "Error in opening file" );

exit();

}
PHP File System Functions:
• fclose():

After making a changes to the opened file it is important to close it with the fclose() function.

The fclose() function requires a file pointer as its argument and then returns true when the
closure succeeds or false if it fails.

Syntax: fclose($handle);

Here,

“fclose” is the PHP function for closing an open file

“$handle” is the file pointer resource


PHP File System Functions:
READING A FILE

• fread():

Once a file is opened using fopen() function it can be read with a function called fread().

This function requires two arguments. These must be the file pointer and the length of the file
expressed in bytes.

The function will stop at the end of the file or when it reaches the specified length, whichever
comes first.

Syntax

fread(file, length);
PHP File System Functions:
Example;

To read entire file:

<?php

$file = fopen("test.txt","r");

fread($file,filesize("test.txt"));

fclose($file);

?>

The files length can be found using the filesize() function which takes the file name as its
argument and returns the size of the file expressed in bytes.
PHP File System Functions:
• file_get_contents()

This function will read the entire contents of a file into a variable without the need to open or
close the file.

Example:

<?php

$f = "c:\windows\win.ini";

$f1 = file_get_contents($f);

echo $f1;

?>
PHP File System Functions:
WRITING A FILE:

• fwrite():

The fwrite() function in PHP is an inbuilt function which is used to write to an open file.

The fwrite() function stops at the end of the file or when it reaches the specified length passed as
a parameter, whichever comes first.

Syntax: fwrite(filepointer, $string, $length);

Here,
“fwrite” is the PHP function for writing to files
“$filepointer” is the file pointer resource
“$string” is the data to be written in the file.
“$length” is optional, can be used to specify the maximum file length.
PHP File System Functions:
Example:

<?php

$file = fopen("test.txt","w");

echo fwrite($file,"Hello World. Testing!");

fclose($file);

?>

The output of the code above will be:

21
PHP File System Functions:
• file_put_contents():

This function is used to write the new contents into a file.

First, it deletes the existing contents of a file then it writes new contents.

If file is not available it creates a new file.

Syntax: file_put_contents(filename, data, mode, context)

Filename - Specifies the path to the file to write to. If the file does not exist, this function will
create one

Data - The data to write to the file. Can be a string, array, or a data stream

Mode - Optional. Specifies how to open/write to the file. Possible values:

FILE_USE_INCLUDE_PATH - search for filename in the include directory


PHP File System Functions:
FILE_APPEND - if file already exists, append the data to it - instead of overwriting it

LOCK_EX - Put an exclusive lock on the file while writing to it

context - Optional. Specifies the context of the file handle. Context is a set of options that can
modify the behavior of a stream.

<?php

echo file_put_contents("test.txt","Hello World. Testing!");

?>

Output:

21
PHP File System Functions:
• fputs( ):

The fputs() function in PHP is an inbuilt function which is used to write to an open file. The
fputs() function stops at the end of the file or when it reaches the specified length passed as a
parameter, whichever comes first.

Syntax: fputs(filepointer, string, length)

The fputs() function in PHP accepts three parameters.

file: It is a mandatory parameter which specifies the file.

string : It is a mandatory parameter which specifies the string to be written.

length : It is an optional parameter which specifies the maximum number of

bytes to be written.
PHP File System Functions:
Example:

<?php

$file = fopen("test.txt","w");

echo fputs($file,"Hello World. Testing!");

fclose($file);

?>

output: 21
PHP File System Functions:
COPYING A FILE

The PHP copy function is used to copy files.

Syntax: copy(from_file, to_file, context)

Here,

from_file - Specifies the path to the file to copy from

to_file - Specifies the path to the file to copy to

context - Specifies a context resource created with stream_context_create()

Example: <?php

echo copy("source.txt","target.txt");
PHP File System Functions:
DELETING A FILE:

The unlink function is used to delete the file.

Example:

<?php

$file = fopen("test.txt","w");

echo fwrite($file,"Hello World. Testing!");

fclose($file);

unlink("test.txt");

?>
PHP File System Functions:
Few more file function:

filetype() Returns the type of the file.

is_dir() Checks whether the file is a directory.

is_executable() Checks whether the file is executable.

rmdir() Removes an empty directory.

readfile() Read the contents of file and write those contents in

current buffer.

You might also like