Creating .php files using PHP

Hi all

What I am trying to create is a page with a text field and a submit button, which when you click submit would enter the text into a freshly create .php file.

What I need is the page name to generate itself e.g.

user one goe’s to the page clicks submit and the information is entered into 1.php, then the next user goe’s to the page and clicks submit it would input the text into 2.php and so on.

here is the code so far:

<?php $kb = include("kbnum.php"); $file = fopen("$kb.php","w+");

?>

information: <?php $text = $_POST['info']; fwrite($file, $text); fclose($file); $kbnum = (1 + $kb); $kb = $kbnum; $file2 = fopen("kbnum.php","w+"); fwrite($file2,$kbnum). "
"; fclose($file2); ?>

So what I am looking to achieve with this is
The page calls the text in kbnum.php - which would be one to begin with
and stores it in $kb
The creates a using $kb.php which makes the file called 1.php
Then all the data is entered into this file and closed
Then page increments $kb by 1 and writes it to the file kbnum.php
then process would repeat over and over every time somebody hits submit

Unfortunally this isn’t working could someone help?

I suppose storing the “counter” variable in database would be a good idea. Fetch the counter from database, increment it, write back to database and create the file.

Alternately, you can also store the counter variable in a separate file (say counter.txt). After form submission you can get the value from counter.txt, lets say 10, increment it by 1, write it back to counter.txt and then create a new file called 10.php for storing the form submission data.

Else, a better approach would be to save file name using “user_name current date time.txt”

Personally, I would prefer database way.

Well i tried to store the number into kbnum.php but after it writes the first time and increments it too 2 its stops working

I told you, database is the best option. Try it!

Try this:

test.php
[php]

Submit

[/php]

form_process.php
[php]<?php
error_reporting(E_ALL);
if(isset($_POST[‘submit’])){
$counter_file = “people.txt”;
flock($counter_file, LOCK_EX);
$counter = file_get_contents($counter_file);
$count = $counter;
$counter = $counter + 1;
$return = file_put_contents(“people.txt”, $counter);

flock($counter_file, LOCK_UN);
if($counter != $count){
	$file = $count.".php";
	file_put_contents($file, $_POST['anyText']);
}
else
	echo "Error";

}
?>
[/php]

Thanks alot!

This worked a treat! :slight_smile:

Sorry to be a pain but do you know how a would move the file to another location?

yes, good and i want a “treat” from you as well!! :slight_smile: :smiley:

for moving files you can use rename() OR copy() followed by unlink()

http://php.net/manual/en/function.rename.php

http://www.php.net/manual/en/function.copy.php

http://www.php.net/manual/en/function.unlink.php

Sponsor our Newsletter | Privacy Policy | Terms of Service