0

hello again,
okay so what im tring to do is have text field that when you input text and submit , it creates new file with text value as the name.
as this shows this inputs the text into the file and randomly generates a # for the name of the file.
I have tried differant ways of doing this but just cant get it. any help would be nice.

<html>
    <body>
<form name="form" method="post">
                    <input type="text" value="<?php echo htmlspecialchars($main_topic); ?>"

 name="text_box" size="50"/>
            <input type="submit" id="search-submit" value="NEW TOPIC" />
        </form>
    </body>
</html>
<?php
    if(isset($_POST['text_box'])) { //only do file operations when appropriate
        $a = $_POST['text_box'];
        $n1 = rand (1, 99);
        $myFile = "$n1.php";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $a);
        fclose($fh);
    }
?>
3
Contributors
4
Replies
16
Views
5 Years
Discussion Span
Last Post by Tko_1
0

not sure how clean this is but it works..

<html>
    <body>
<form name="form" method="post">
                    <input type="text" value="<?php echo htmlspecialchars($text_box); ?>"

 name="text_box" size="50"/>
            <input type="submit" id="search-submit" value="NEW TOPIC" />
        </form>
    </body>
</html>
<?php
    if(isset($_POST['text_box'])) { //only do file operations when appropriate
        $a = $_POST['text_box'];
        $ext = '.php';
        $both = $_POST['text_box']. "" . $ext;
        $fh = fopen($both, 'w') or die("can't open file");
        fwrite($fh, $a);
        fclose($fh);
    }
?>

now anyone know how to include php file within this script ??

0

how to include php file within this script ??

Why do you want to include a php file? If you want to include any php file then

include_once('filename.php')
0

You could also use the simplified version of writing to a file, the file_put_contents(file_name, file_contents) function (click here to see it on php.net) for this purpose. You could then use the input from your form to determine the file name and content.

What exactly is going wrong, by the way?

Oh, and another remark: generating a random name like that (using a random number ranging from 0 to 100) is not very secure: you will end up sometime with overwriting an existing file. If you do want to use this method of generating a random file name, I suggest checking if it already exists before you try to write to it. Because if it does already exist, you might encouter some errors in your script.

Edited by minitauros

0

thanks guys,
my thought was to create a file that is already populated with script or include a template file to it as its being made and name it. seems harder than i thought.

thanks minitauros slipped my mind bout the overwite.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.