School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,172 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




File Generation - DETAILED Creation + Writing

 
Reply to this topicStart new topic

> File Generation - DETAILED Creation + Writing

Rating  5
pr4y
Group Icon



post 20 Sep, 2008 - 01:09 PM
Post #1


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);
}
?>


whatsthat.gif 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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

ment0r
*



post 23 Sep, 2008 - 05:11 AM
Post #2
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
Go to the top of the page
+Quote Post

pr4y
Group Icon



post 23 Sep, 2008 - 04:27 PM
Post #3
your welcome ment0r smile.gif

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 smile.gif

OpenSource for life.
Go to the top of the page
+Quote Post

Nykc
Group Icon



post 25 Sep, 2008 - 10:56 AM
Post #4
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!!!!
Go to the top of the page
+Quote Post

pr4y
Group Icon



post 25 Sep, 2008 - 11:26 AM
Post #5
QUOTE(Nykc @ 25 Sep, 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!!!!


First off, thanks for the complement! biggrin.gif

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 tongue.gif

Cheers!
Go to the top of the page
+Quote Post

xGodDogx
*



post 27 Mar, 2009 - 10:30 PM
Post #6
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?
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:58PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month