CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2006
    Posts
    228

    Question Create File using PHP

    hello..

    just wondering how do you create a file using php?

    I have a text box on a page and I want to make it when you enter something making it create a file on my website called what ever is in the text box.

    For Example:

    I type in 'test' in the textbox and when I click the submit button making it create the 'test' file with .txt at the end. (test.txt)

    any ideas?

  2. #2
    Join Date
    Aug 2002
    Posts
    879

    Re: Create File using PHP

    check-out "fopen" on php.net for this
    http://de2.php.net/manual/en/function.fopen.php

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: Create File using PHP

    And after that you will need fwrite().

    Here is the example code from that page.
    PHP Code:
    <?php
    $filename 
    'test.txt';
    $somecontent "Add this to the file\n";

    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {

       
    // In our example we're opening $filename in append mode.
       // The file pointer is at the bottom of the file hence 
       // that's where $somecontent will go when we fwrite() it.
       
    if (!$handle fopen($filename'a')) {
             echo 
    "Cannot open file ($filename)";
             exit;
       }

       
    // Write $somecontent to our opened file.
       
    if (fwrite($handle$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";
    }
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    Resolved Re: Create File using PHP

    If you use PHP 5, try this (not tested):


    <html>
    <head><title>Create Files</title></head>
    <body>
    <?php
    if (isset($_POST['filename'])) {
    $myfile = urlencode($_POST['filename']);
    $mycontent = urlencode($_POST['filecontent']);
    file_put_contents($myfile, $mycontent);
    echo 'File created sucessfuly!';
    }

    echo '<form action="'.$_SERVER['PHP_SELF'].'">
    File name: <input type="text" name="filename"><br>
    File content: <input type="text" name="filecontent"><br>
    <input type="submit" value="Submit">
    </form>';
    ?>

    </body>
    </html>
    All consequences are eternal in some way.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured