Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Creating Files

    I have a form
    Code:
        <form action="" method="">File name:
        <input name="filename" type="text" size="30" maxlength="47" />
        <br />
        <input name="name" type="submit" value="Submit" />
        </form>
    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:
    <?PHP

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

    ?>
    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
    Regular Coder
    Join Date
    Jan 2003
    Posts
    400
    Thanks
    0
    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:
    [code]
    <?php
    $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>";
    exit;
    }

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

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

    fclose($handle);

    } else {
    echo "The file $filename is not writable";
    }
    }
    ?>
    [\code]

    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
    •