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 4 of 4
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Question Creating a txt file in PHP?

    Hello,
    I'm trying to create txt files in php,
    I've found this page on the php manual:
    http://ca3.php.net/fwrite

    but this only write text to a txt file, and I need to know how to create the txt file, then write to it,

    my fwrite code:
    PHP Code:

    <?php
    $filename 
    'test.txt';
    $somecontent "Add this to the file\n";

    if (
    is_writable($filename)) {

       if (!
    $handle fopen($filename'a')) {
             echo 
    "Cannot open file ($filename)";
             exit;
       }

       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";
    }
    ?>

    any ideas how i can do this?

    thanks in advance for your time!
    -Ken

  • #2
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    fopen() creates the file if it doesn't exist.

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ah, no it doesn't.

    "x" must be included.
    or in my case "x+"

    like:

    fopen($filename,"x+");


    -Ken

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    one more question though,

    I want to populate the txt file or html file rather with content from my data base,

    so I'm wondering how this can be done?

    example:


    PHP Code:

    <?php
    $dirname  
    'test';

    mkdir("/Program Files/xampp/htdocs/dating/$dirname"0700);

    $filename "../$dirname/index.html";

    if (
    is_writable($filename)) {
    @
    unlink($filename);
    }

    fopen($filename,"x+");

       if (!
    $handle fopen($filename'a')) {
             echo 
    "Cannot open file ($filename)";
             exit;
       }

    $link mysql_connect($SqlHost$SqlUser$SqlPass) or die('Login details incorrect');
    $db_selected mysql_select_db($SqlDb$link) or die('Database name incorrect');
    $result mysql_query("SELECT * FROM subcategories WHERE cid=$_REQUEST[cat] ORDER BY subcategorie asc");

    fwrite($handle"
    while ($row = mysql_fetch_array($result))
    {
    <font face=verdana size=1>[<a href='func.php?func=delsubcat&scid=<? echo $row[scid]; ?>&cat=<? echo $_REQUEST[cat]; ?>'>delete</a>] </font><font face=verdana size=2><? echo $row[subcategorie]; ?></font><br />
    }
    "
    );

       
    #echo "Success, generating html to files ($filename)";

       
    fclose($handle);

        
    header("Location: ../$dirname");
    ?>
    something like that,
    but i know what can't be done as is.
    any ideas?

    thanks again for your time!
    -Ken


  •  

    Posting Permissions

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