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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating a .txt file with PHP

    Creating a ".txt" file (or any text file for that matter) can be done simply with PHP.

    So lets start shall we?
    What will we be using?
    fopen();
    fwrite();
    fclose();
    strlen();

    PHP Code:
    <?php
    $filename 
    "test01.txt"//the name of our file.
    $content "This is our test file"//what we will be writing to our file.
    $strlength strlen($content); //gets the length of our $content string.
    $create fopen($filename"w"); //uses fopen to create our file.
    $write fwrite($create$content$strlength); //writes our string to our file.
    $close fclose($create); //closes our file
    echo("File Created, Click <a href='$filename'> Here </a> to view it.");
    ?>
    This is just very basic, i hope it can help someone.
    Last edited by [_bushido_]; 02-26-2006 at 02:03 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Is it me or did somebody just open a can of tasty luncheon meat in here?

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Pennimus
    Is it me or did somebody just open a can of tasty luncheon meat in here?
    huh? lol

  • #4
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is more of a forum for posting questions and problems.

    There is another forum for code snippets.

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks !
    thats nearly exactly what i was going to come here and ask

    cheers !

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    The fwrite() example code in the php manual http://us2.php.net/manual/en/function.fwrite.php is several times better than the posted code as the code in the manual includes error checking and error reporting, which is often neglected in sample code, tutorials, and by beginning programmers.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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