Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Creating Files

  1. #1
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Thanked 7 Times in 7 Posts

    Creating Files

    I have a form
        <form action="" method="">File name:
        <input name="filename" type="text" size="30" maxlength="47" />
        <br />
        <input name="name" type="submit" value="Submit" />
    I want to have a script which checks if [whatever they put in the form].php exists in /, if it doesn't, then I want it to create a file in / called [whatever they put in the form].php and a file in /files called [whatever they put in the form].txt, then put in /[whatever they put in the form].php
    PHP Code:

    require ('files/topheader.txt');
    require (
    require (
    'files/[whatever they put in the form]');
    require (
    require (

    If the file already exists I want the script to
    PHP Code:
    echo ('<p class="error">File Already Exist</p>'
    Last edited by gsnedders; 03-26-2004 at 09:02 PM.

  2. #2
    Regular Coder
    Join Date
    Jan 2003
    Thanked 0 Times in 0 Posts
    Before you ask PHP questions, try and take a look at www.PHP.net
    But... heres what you would need to do:
    $filename = $_(post/get)[(whatever they put in the form)] . ".php";

    if (file_exists($filename)) {
    echo "<p class=\"error\">File Already Exist</p>";
    } else {
    if (!$handle = fopen($filename, 'a')) {
    echo "<p class=\"error\">There was an error</p>";

    // Write $somecontent to our opened file.
    if (fwrite($filename, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";

    echo "Success, wrote ($somecontent) to file ($filename)";


    } else {
    echo "The file $filename is not writable";

    Its that or something extremely close. That script I just gave you does not write the .txt file just so you know. This is just a vague idea of what you need to write. Visit www.php.net and read some of the documentation. Its a great php learning thing. Also try and buy a few reference manuals (books on PHP) off of www.half.com .


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts