## Topic outline

• ### PHP FILES & IO

• Lesson

We can store all data on web server by two methods

1) By using MYSQL

2) By using files, it can be quicker and more convenient to directly access files on the hard disk.

Here in this tutorial, we are going to see briefly about second method.

OPENING A FILE

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

The following code illustrates opening a file

<!DOCTYPE html>

<html>

<body>

<?php

$myfile = fopen("abc.txt", "r") or die("Unable to open file!"); echo fread($myfile, filesize("abc.txt"));

fclose($myfile); ?> </body> </html> The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read. The following PHP code reads the "webdictionary.txt" file to the end: fread($myfile, filesize(“abc.txt"));

WRITE TO FILE

The fwrite() function is used to write to a file.

The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written..

The following code illustrates to write a file in PHP

<!DOCTYPE html>

<html>

<body>

<?php

$myfile = fopen("newfile.txt", "w","r") or die("Unable to open file!");$txt = "John Doe\n";

fwrite($myfile,$txt);

$txt = "Jane Doe\n"; fwrite($myfile, $txt); echo fread($myfile, filesize("newfile.txt"));

fclose($myfile); ?> </body> </html> CHECKING WHETHER FILE EXISTS To determine whether a file already exists, you can use the file_exists function, which returns either TRUE or FALSE, and is used like this: if (file_exists("testfile.txt")) echo "File exists"; The following code illustrates to create in PHP <?php // testfile.php$fh = fopen("testfile.txt", 'w') or die("Failed to create file");

$text = <<<_END Line 1 Line 2 Line 3 _END; fwrite($fh, $text) or die("Could not write to file"); fclose($fh);

echo "File 'testfile.txt' written successfully";

?>

When we run this in a browser, all being well, you will receive the message File 'testfile.txt' written successfully. If you receive an error message, your hard disk may be full or, more likely, you may not have permission to create or write to the file, in which case you should modify the attributes of the destination folder according to your operating system.

This simple example shows the sequence that all file handling takes:

1. Always start by opening the file. You do this through a call to fopen.

2. Then you can call other functions; here we write to the file (fwrite), but you can also read from an existing file (fread or fgets) and do other things.

3. Finish by closing the file (fclose).

There are several different parameter that can be used for reading, writing and appending a file

 MODE ACTION DESCRIPTION r Read file from start Open for reading only, place the file pointer at the beginning of the file r+ Read file from start and allow writing Open for reading and writing, place the file pointer at the beginning of the file w Write file from start and truncate file Open for writing only w+ Write file from start, truncate file and allow reading Open for reading and writing a Append to file end Open for writing only a+ Append to file end and allow reading Open for reading and writing

APPEND A FILE IN PHP:

The following code illustrates how to append a file in PHP:

<!DOCTYPE html>

<html>

<body>

<?php

$myFile = "testFile.txt";$fh = fopen($myFile, 'a') or die("can't open file");$stringData = "New Stuff 1\n";

fwrite($fh,$stringData);

$stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh);

?>

</body>

</html>

COPYING A FILE IN PHP

PHP copy function used to create clone of test file in PHP.

The following is the sample code to copy a file in PHP

<?php

// copyfile.php

copy('testfile.txt', 'testfile2.txt') or die("Could not copy file");

echo "File successfully copied to 'testfile2.txt'";

?>

DELETING A FILE

By using unlink() function ,we will remove  a file from our system.

The following sample code help to delete a file

<?php

// deletefile.php

echo "Could not delete file";

else

echo "File 'testfile2.new' successfully deleted";

?>

file_get_content() function is used for reading the entire file without using a file handler.

The following code illustrates how to read a file by using function file_get-content()

<?php

echo "<pre>";  // Enables display of line feeds

echo file_get_contents("testfile.txt");

echo "</pre>"; // Terminates pre tag

?>