Reading Files PHP Beyond Basics

Reading Files PHP Beyond Basics

PHP, TUTORIALS

Reading Files PHP Beyond Basics




Welcome to another fun tutorial in the PHP Beyond Basics Series. In this lesson Reading Files PHP Beyond Basics we are gonna learn how to read back the contents from the file we already stored. Here we can use many different ways to achieve this, and lets find which are all the easiest methods from the real time examples.
The simplest way to do this is using the fhandle() function to handle this situation. This handle taking the filename and read mode as the parameters and open the file for us.

Reading Files PHP Beyond Basics
In the above simple example we declare the variable named $file and initialize that with the file name called testfile.txt  . And we open the file using the fopen()   function to view the file. In order to view the file contents we have to open that file.
After open the file we read the file using the fread()   function. This takes the $handle and the number bytes we wants to read as a parameter.
Finally we show the content using the echo function like we did in the above example. In this example we can also see how to entire file using the different functions. Lets see them also for our various purposes in the future uses.

The above shown is the example for how to read more than one line of file contents at the same time. Here we fixed the limit as 20 bytes.

Reading Files PHP Beyond Basics
The above shown is the example for how to read the entire file and displays all as it is in the source files.
In this Reading Files PHP Beyond Basics. Like we did in writing files we use file_put_contents() function for save the contents in a shortcut. Like that we can use the shortcut function to read the contents from the file using the file_get_contents(). Lets see how to do that.

Reading Files PHP Beyond Basics
The above example is also shows the entire content of the files.

Reading Files PHP Beyond Basics

In this lesson we additionally learn how to delete the file from the directory. We simply use the single function to drop the files. Lets do that by an real time example.
We must close the files first. Because we can’t delete the open files. So make sure before you delete the file is close or open. And next is we should have the delete permission to delete that file. Otherwise we can’t delete the file.

If the file has been deleted then the above output is shown else it asks to close your file and don’t delete the file.
We hope this Reading Files PHP Beyond Basics lesson will gives you an clear knowledge about how to Reading content from file and delete a files.
Download the source code for Reading Files PHP Beyond Basics Reading Files.

Leave a Reply