Tutorials PHP Reading another file

Reading another file

Today you will learn how to display the contents of another file.

How do I display a file?

One simple line will do the trick.

echo file_get_contents("file.txt");

What files can I read?

Pretty much any plain text file. HTML, TXT and PHP will work fine.

Does this process code?

The server won't process the code but your browser will. So server side code like PHP and MySQL will not be processed and client side code like HTML, CSS and JavaScript will be. If you don't want the browser to process the code, run the contents through htmlEntities with ENT_QUOTES before displaying it.

Should I use file_get_contents or include and require?

Using file_get_contents puts the data as a string. So use include and require if there is PHP that need to be processed and file_get_contents if there is no PHP or you don't want to process the PHP.

Posted by on . Category: PHP


Comments

No comments posted yet

You need to register or login to post new comments.