Open a text file within another text file using PHP

Opening remote text files residing within another

This could be used for a number of things, but what we are taking a look at is opening a text file that contains a set of addresses that point to other text files, that we will open and use a loop to display all the contents of 4 text files on the same page.

It make take the demo a moment to load due to the contents of the files that are being loaded, this could be taken care of by giving your users some visual feedback (yahoo loadbar) or something like that.

Anyway, what we will do at first is to create a function that we will include at the top of the page:

<?php
function showTxt($fn){
$fop = fopen($fn, "r");
$html = "";
while(!feof($fop)){
$newFn = fgets($fop);
$html.= $newFn . '<br/>';
}
return $html;
}
?>

All the above does is when we call it, is return the values of the addresses we grabbed from the initial text file.

Here are the contents of the initial text file:

http://slaper.be/Gegevens_Installatie.txt
http://slaper.be/Fjord/Gegevens_Installatie.txt
http://zon.cindyenguy.be/opbrengsten/Gegevens_Installatie.txt
http://eerdekens.com/zon/Gegevens_Installatie.txt

Ok, now we are all set to read the initial file and use the function:

This is the main snippet that will open the initial file and then use the function in a loop that will give us the desired effects.

$fp = fopen("initial_text_file.txt", "r");

while (!feof($fp)) {
// process current line
$current_line = fgets($fp);
echo '<h5>'. $current_line . '</h5>';
echo showTxt($current_line);
}
fclose($fp);

At first we open the file: $fp = fopen("initial_text_file.txt", "r");

And then we use a loop to pull the info and use again later.

while (!feof($fp)) {

This line uses the function we created and placed at the top of the page:

echo showTxt($current_line);

All it does is display the $html variable we created in the function showTxt and put it on the screen.

And then we close the connection, always.

fclose($fp);

Entire script:

<?php
function showTxt($fn){
$fop = fopen($fn, "r");
$html = "";
while(!feof($fop)){
$newFn = fgets($fop);
$html.= $newFn . '<br/>';
}
return $html;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?
$fp = fopen("initial_text_file.txt", "r");

while (!feof($fp)) {
// process current line
$current_line = fgets($fp);
echo '<h5>'. $current_line . '</h5>';
echo showTxt($current_line);
}
fclose($fp);
?>
</body>
</html>

I know this was short and sweet, but if you have any questions about what is here, let me know, and I welcome any feedback to create a better way to do this.

Thank you for reading!


More by this Author


Comments 2 comments

sohaibshaheen profile image

sohaibshaheen 6 years ago from Pakistan

I think you should be little descriptive in your approach. Otherwise its nice.


psychicdog.net profile image

psychicdog.net 6 years ago

great read - highly economical programming. Thanks Alpho11

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working