Jump to content

cbox


Beginning PHP - Lesson 17 - Reading External Files into PHP


fattwam

Recommended Posts

Posted

Now lets put what we learned about the explode() function and arrays into practice parsing (dealing with) files. Lets make a file called "file.txt" and fill it with at least 5 lines of non-sense. Open up SciTE or Notepad and type the following:

 

This is line 1
This is line 2
This is line   ...uh... (what is after 2?)
anyways, this is the next line
And this is the last line!

Save the file and up load it to your web server.

 

 

Make an new make a new file called "filetoarray.php". After you save it type the following into it:

 


// Store the file name in a string.
$filename = "file.txt";

// Read file into array
$contents = file($filename);

echo "there are [b]". count($contents). "[/b] lines in this file. Here is what he array looks like now:
"; 

print_r($contents);

echo "[b]Below are the lines in the file after they were "shuffled":[/b]
";
shuffle($contents);

// Loop through the array and print each line to the screen.
foreach ($contents as $line)
{
  echo $line. "
";
}

echo "Now the SHUFFLED array looks like this:
";
print_r($contents);
?>

Then save it and upload it to the same directory as "file.txt".

 

After you run the file you should see two parts to the script. First is the Number of lines that are in the file and a print-out of what array element each line is in. Second you should see the lines you typed in file.txt showing up in a random order every time you refresh the page, with the print-out of what array element they are below them. So lets go over them!

 

 

// Store the file name in a string.
$filename = "file.txt";

In this first part of the code we store the name of a file in a variable.

 

// Read file into array
$contents = file($filename);

Next we use the "file" function to read every line out of "file.txt" into and array called "$contents". Every line will be a different element it the array. For example, line one will be the first element in the array:

Array ([0] => This is line 1);
//Remember arrays start at "0" and go up for each element.

Line two will be the second element in the array

Array ([1] => This is line 2);

And so on....

Each new line will be put into a new element of the array. Since my file.txt has five lines there will be five elements in the array. (0,1,2,3,4 because arrays start at 0)

 

Next we will use the count() function to count how many elements are in the array:

echo "there are [b]". count($contents). "[/b] lines in this file. Here is what he array looks like now:
"; 

(Note: Remember, there are the same number of lines in the file as there are elements in the array so counting the elements in the array will tell us the number of lines in the file. Confused yet?)

 

 

echo "[b]Below are the lines in the file after they were "shuffled":[/b]
";
shuffle($contents);

In the above code we then use the shuffle() function to randomly shuffle the elements in the array. So element "0" might end up as element "3" and so on. End the end we will still have five elements in the array... they'll just be in different places!

 

 

Last, we use a foreach loop to go through each element in the array and print out the line:

// Loop through the array and print each line to the screen.
foreach ($contents as $line)
{
  echo $line. "
";
}

echo "Now the SHUFFLED array looks like this:
";
print_r($contents);

We end by using the print_r function to show you what the array looks like after it has been shuffled.

 

 

##################################################################

 

Now lets make something useful out of this....I know! how about a random quote?

(Like the one you see at the bottom of this page!)

Change the code in "filetoarray.php" to this:


// Store the file name in a string.
$filename = "file.txt";

// Read file into array
$contents = file($filename);

// Shuffle the elements to make the array random
shuffle($contents);

// Print out the first element.
echo $contents[0];

?>

This is pretty simple to follow so I am just going to review some of it. After we have read each line into an array called "$contents" we just shuffle the arrays elements to make them random and print out the first element. (Which would be one of the lines in the file.)

 

 

#######################################################

 

You could also use something like this to choose a random banner or affiliate! So that is your home work - I want you to try to figure out how you could make a random image appear. I'll give you a major hint, 8) change "file.txt" to contain a new HTML image tag () for each line!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...