Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryTutorials and Answers

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Importing Any Content From an External File Using PHP

Text files, HTML files, the content of any available file can be imported and published directly onto a web page with the PHP readfile() function. Example:

<?php readfile("myfile.txt"); ?>

"myfile.txt" is the location of the file to import. See further below for more information about specifying the file location.

With the readfile() function, the content of the file is inserted into the web page for delivery to the browser.

The inserted content is literal, no interpretation is done before inserting the imported content into the web page. As an example, importing a file of PHP code results in the PHP code itself being published. As another example, importing an image publishes the code in the image file instead of the visible image.

Note: The browser itself will interpret HTML tags and other characters and entities of imported content like it does for a web page. But the PHP code does no interpretation before inserting the imported content — unless the file is imported with a http/https URL (see further below), in which case the PHP code is interpreted at the source browser before it is imported.

Thus, readfile() generally is useful only for human-readable content. Still, there may be times when a person actually does want to publish the content of a code file.

Specifying File Location

The file location needs to be specified for readfile() to work. When the location is specified as "myfile.txt", it is assumed file myfile.txt is in the same directory as the PHP script doing the importing.

The location can be specified as:

  • Relative to the directory where the PHP script doing the importing is located.

  • The server path to the directory and file.

Specified as relative location.

For relative locations, "./" and an absence of "/" both mean "the current directory." Thus, "./myfile.txt" and "myfile.txt" resolve to the same location, file myfile.txt in the directory where the PHP script is running.

The relative location "../" means the parent directory, the directory next closer to document root.

Specified as server path location.

When the PHP script containing a call to the readfile() function may be moved to different directories, it is generally better to specify a server path location than a relative location.

The following assumes the PHP script calling the readfile() function is located on domain example.com —

To determine the server path location, first determine the URI of the file to import. That would be the URL with the http and domain name parts removed. For example, URL http://example.com/subdir/myfile.txt has URI /subdir/myfile.txt

Then, prepend the location of the document root directory to it. (The document root directory is the directory where the domain's home or index page is located.)

For ease of implementation and to make the PHP script portable to other servers, use the PHP variable that contains the location of the document root directory: $_SERVER["DOCUMENT_ROOT"]

Thus, this command will import the file located at http://example.com/subdir/myfile.txt

<?php readfile($_SERVER["DOCUMENT_ROOT"]."/subdir/myfile.txt"); ?>

Specifying File Location as URL

Some servers have PHP configured so the readfile() function can import files with a URL as the file location.

To test your PHP configuration, put this in a file named test.php and upload it to your server.

<?php readfile("http://example.com/"); ?>

Type the URL of the text.php file containing the above line into your browser. If content other than error message displays in your browser window, your PHP can work with URLs in readfile(). Otherwise, not.

Being able to use URLs in readfile() means content can be obtained at any valid URL anywhere on the internet, provided the server at the URL has no blocks in place to prevent it.

The PHP readfile() function is good for importing the content of any file and inserting it into a web page without interpretation.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC