Page 1 of 1

File Generation - DETAILED Creation + Writing Rate Topic: ***** 2 Votes

#1 pr4y   User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Posted 20 September 2008 - 02:09 PM

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
$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.

$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:

// $filetype = ".php";



And changing:

	$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.

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.

$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.

	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.

	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
$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 September 2008 - 07:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: File Generation - DETAILED Creation + Writing

#2 ment0r   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-September 08

Posted 23 September 2008 - 06:11 AM

WOW! I can't believe I've been searching for something that does this for almost a month now. Nothing important that I need done, but I needed a way to create large amounts of files for my website rather than having to use cPanel to create them all individually (takes FOREVER)

Thank you so much!!! I didn't even copy + paste this either, I actually took my time reading it and learned my first bit of PHP, you are the greatest.

<3
Was This Post Helpful? 0
  • +
  • -

#3 pr4y   User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Posted 23 September 2008 - 05:27 PM

your welcome ment0r :)

seems like you had the same trouble I had... but I was driven to write my own, I had no clue it would of been this short of a script with PHP... just cant believe the available software for this type of thing is a minimum of $29.99...

glad i saved one person money, hopefully more :)

OpenSource for life.
Was This Post Helpful? 0
  • +
  • -

#4 Nykc   User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 740
  • View blog
  • Posts: 8,654
  • Joined: 14-September 07

Posted 25 September 2008 - 11:56 AM

Hey I like this guy already.....

Very nice tutorial. I was trying to do something similiar in PERL. PHP works for me as well..

Gracias!!!!
Was This Post Helpful? 0
  • +
  • -

#5 pr4y   User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Posted 25 September 2008 - 12:26 PM

View PostNykc, on 25 Sep, 2008 - 11:56 AM, said:

Hey I like this guy already.....

Very nice tutorial. I was trying to do something similiar in PERL. PHP works for me as well..

Gracias!!!!


First off, thanks for the complement! :D

Second, just thought you should note that this script only supports text files up to 200kb in size. When I tested this, I used a text file with 500 file names... and it was only 9.8kb..... so unless you need to generate more than 10,000 files (LOL) you might need to use arrays and multiple functions to complete what you need done... but I doubt anyone will ever need to generate more than 10,000 files, so this works for now :P

Cheers!
Was This Post Helpful? 0
  • +
  • -

#6 xGodDogx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-March 09

Posted 27 March 2009 - 11:30 PM

so im kinda wondering how this works.... im not to sure about this ive just started coding as well and just wondering so under the file thing would this be where you put the names you wanted and it would create all files or would this like create 1 file at a time and you would have to specify what the names are everytime it runs the script?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1