0% found this document useful (0 votes)
13 views

Unit IV

This chapter explains how to open, read, write, and close files in PHP. It discusses the fopen(), fread(), fwrite(), and fclose() functions. It provides examples of opening a file for reading, reading the file contents into a variable, writing text to a new file, and closing the file. It also briefly explains the copy(), rename(), and unlink() functions for copying, renaming, and deleting files.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit IV

This chapter explains how to open, read, write, and close files in PHP. It discusses the fopen(), fread(), fwrite(), and fclose() functions. It provides examples of opening a file for reading, reading the file contents into a variable, writing text to a new file, and closing the file. It also briefly explains the copy(), rename(), and unlink() functions for copying, renaming, and deleting files.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

This chapter will explain following functions related to files −

 Opening a file
 Reading a file
 Writing a file
 Closing a file

Opening and Closing Files


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.

Files modes can be specified as one of the six options in this table.

Sr.No Mode & Purpose


r

1 Opens the file for reading only.

Places the file pointer at the beginning of the file.


r+

2 Opens the file for reading and writing.

Places the file pointer at the beginning of the file.


w

Opens the file for writing only.

3 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.


w+

Opens the file for reading and writing only.

4 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.


5 a
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.


6
Places the file pointer at the end of the file.

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

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.

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.

Reading a file
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 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.

So here are the steps required to read a file with PHP.

 Open a file using fopen() function.


 Get the file's length using filesize() function.
 Read the file's content using fread() function.
 Close the file with fclose() function.

The following example assigns the content of a text file to a variable then displays those contents
on the web page.

<html>

<head>
<title>Reading a file using PHP</title>
</head>

<body>
<?php
$filename = "tmp.txt";
$file = fopen( $filename, "r" );

if( $file == false ) {


echo ( "Error in opening file" );
exit();
}

$filesize = filesize( $filename );


$filetext = fread( $file, $filesize );
fclose( $file );

echo ( "File size : $filesize bytes" );


echo ( "<pre>$filetext</pre>" );
?>

</body>
</html>

It will produce the following result −

Writing a file
A new file can be written or text can be appended to an existing file using the PHP fwrite()
function. This function requires two arguments specifying a file pointer and the string of data
that is to be written. Optionally a third integer argument can be included to specify the length of
the data to write. If the third argument is included, writing would will stop after the specified
length has been reached.

The following example creates a new text file then writes a short text heading inside it. After
closing this file its existence is confirmed using file_exist() function which takes file name as an
argument

<?php
$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );

if( $file == false ) {


echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
?>
<html>

<head>
<title>Writing a file using PHP</title>
</head>

<body>

<?php
$filename = "newfile.txt";
$file = fopen( $filename, "r" );

if( $file == false ) {


echo ( "Error in opening file" );
exit();
}

$filesize = filesize( $filename );


$filetext = fread( $file, $filesize );

fclose( $file );

echo ( "File size : $filesize bytes" );


echo ( "$filetext" );
echo("file name: $filename");
?>

</body>
</html>

It will produce the following result −

PHP copy(), rename() and unlink() file handling


In this php tutorial we will learn how use copy() function, rename() function and unlink()
function with php example.
PHP File handling functions :

 copy() – used to copy a file.


 rename() – used to rename a file.
 unlink() – used to delete a file.

PHP copy() Function


The copy() function is used to copy a file.

copy() function syntax

<?php

copy(“source filename”, “destination filename”);

?>

copy() function example


PHP copy() example

PHP

<?php

//copy text file


copy("abc.txt","new abc.txt");

1 <?php

2  

3 //copy text file

4 copy("abc.txt","newabc.txt");

5  

6 //open ms word .doc file

7 copy("abc.doc","newabc.doc");

8  
9 //open pdf file

10 copy('abc.pdf',"newabc.pdf");

11  

12 ?>

PHP rename() Function


The rename() function is used to rename a file name.

rename() function syntax

<?php

rename(“old filename”,”new filename”);

?>

rename() function example


PHP rename() example

PHP

<?php

//rename txt file


rename("abc.txt","new abc.txt")

1 <?php

2  

3 //rename txt file

4 rename("abc.txt","newabc.txt");

5  

6 //rename ms word .doc file

7 rename("abc.doc","newabc.doc");
8  

9 //rename pdf file

10 rename('abc.pdf',"newabc.pdf");

11  

12 ?>

PHP unlink() Function


How to Delete a file in PHP.

The unlink() function is used to delete a file.

unlink() function syntax

<?php

unlink(“filename”);

?>

unlink() function example


PHP unlink() example

PHP

<?php

//delete txt file


unlink("abc.txt");

1 <?php

2  

3 //delete txt file

4 unlink("abc.txt");

5  

6 //delete ms word .doc file


7 unlink("abc.doc");

8  

9 //delete pdf file

10 unlink('abc.pdf");

11  

12 ?>

You might also like