First off, I created this script because I was searching around the net for a file generation script that reads from a text file, and the only two I found were $29.99 pieces of shareware. I've long been a believer in Open Source code, and considering the fact that people are out there charging $30 for a 13 line script is disgusting.
php
<?php
$list = "./list.txt";
$lines = file($list);
$filetype = ".php";
$stringData = '<?php include(common.php); ?>';
foreach($lines as $line_num => $line)
{
$FileName = trim($line) . $filetype;
$FileHandle = fopen($FileName, 'w');
fwrite($FileHandle, $stringData);
fclose($FileHandle);
}
?>

That is the code in its entirety. Now to explain why and how everything works the way it does, let me start with your environment variables.
php
$list = "./list.txt";
$lines = file($list);
$filetype = ".php";
$stringData = '<?php include(common.php); ?>';
As you can see, you need to define a few variables. To make things easy when working with the function to actually create the files, we use variables to save time, length, and efficiency.
$list is obviously the file we are reading the information from.
$lines tells the script that $list is a file we will be using
$filetype is an extention of the script that isn't always necessary. Depending on your file list having extentions already in it or not, you can comment this line by using:
php
// $filetype = ".php";
And changing:
php
$FileName = trim($line) . $filetype;
to
$FileName = trim($line);
$stringData is the code that you want in each file. In this case, instead of creating a few hundred pages with static HTML, we will use the PHP include(); function to have one file dynamically loaded throughout the entire file set.
So now I've explained what each variable means... and its time to create our files.
php
foreach($lines as $line_num => $line)
{
$FileName = trim($line) . $filetype;
$FileHandle = fopen($FileName, 'w');
fwrite($FileHandle, $stringData);
fclose($FileHandle);
}
The foreach() function is used to determine how often we execute the following statements, and in this case we are using a text file that is in Line Seperated Variable format.
Again, the $FileName = trim($line) . $filetype; doesn't NEED to have the . $filetype; but in this case our text file doesn't have extentions, so we need to append the $filetype onto the line output.
php
$FileHandle = fopen($FileName, 'w');
What some people don't understand is that when there is no present file to open, PHP automatically creates the file associated. There is no fcreate command. fopen is used to open, AND create files.
The 'w'); in this line is where you define the CHMOD (read / write attributes) of the file. In this case, we use the 'w' attribute to make sure the file will be able to be written. Without this part, the script will fail because it will try to write the file, but by default files are created under the 'r' attribute, which is READ ONLY.
After the fopen(); function, which creates the file itself, we need to write our $stringData to the file.
php
fwrite($FileHandle, $stringData);
The $FileHandle variable is used in this case as opposed to $FileName because fwrite(); requires opening the file when writing. Since the $FileHandle variable uses the fopen(); function, we will use the $FileHandle variable to tell fwrite(); where it is writing to.
$stringData as I explained previously, is the contents of the file we are creating.
php
fclose($FileHandle);
fclose(); MUST be included in this script because if you do not close the file after you create / open / and write the file, the script will likely overflow and / or cause errors.
php
<?php
$list = "./list.txt";
$lines = file($list);
$filetype = ".php";
$stringData = '<?php include(common.php); ?>';
foreach($lines as $line_num => $line)
{
$FileName = trim($line) . $filetype;
$FileHandle = fopen($FileName, 'w');
fwrite($FileHandle, $stringData);
fclose($FileHandle);
}
?>
Well, this has been my first tutorial on this website. Hopefully you will learn something about how files are handled and how to read properly from a text file.
Remember, the reason I wrote this tutorial and am releasing this source to the public is because I am absolutely disgusted at the fact that someone can sell this same exact function for $30. Open Source communities have always given me the help I deserved, but the greedy coders in the world are still profiting off the fact that not everything has been coded for Open Source purposes. I hope you enjoyed this, and if you have any other questions feel free to go ahead and ask!
This post has been edited by pr4y: 22 Sep, 2008 - 06:42 PM