Read and Write txt File

Discussion in 'PHP' started by peter_anderson, Jul 14, 2009.

  1. #1
    Hi,

    I'm trying to create a small script that loads or creates a text file in the /articles directory.

    However, it is not creating a new file if exists, or updating it.

    Here is my code to select the file:
    <form action="admin-ep.php" method="post" name="editpageform">
        <p>
            Page Name:<br />
            <input name="pagename" type="text" /></p>
        <p>
            <input name="EditPage" type="submit" value="Submit" /></p>
    Code (php):
    Here is my code to edit the file:
    <html>
    <head>
    <title>Edit Page :: Powered by rydgroup Club Site</title>
        <style type="text/css">
        body {
        font-family: Helvetica, Arial;
        }
        </style>
    <script type="text/javascript" src="http://www.rydgroup.net/editor/ckeditor.js"></script>
    </head>
    <body>
    
    <h2>
        ADMIN AREA</h2>
    
    <?php 
    
    $pagename = $_POST['pagename'];
    
    $fn = "./articles/$pagename.txt"; 
    $file = fopen($fn, "w+"); 
    $size = filesize($fn); 
    
    if($_POST['Update']) fwrite($file, $_POST['editor1']); 
    
    //$text = fread($file, $size); 
    fclose($file); 
    ?> 
    
    <form action="<?=$PHP_SELF?>" method="post"> 
    
                <textarea name="editor1"><?php $file ?>></textarea>
                <script type="text/javascript">
                    CKEDITOR.replace( 'editor1' );
                </script>
    
    
    <input type="submit" value="Update"/> 
    
    </form>
    Code (php):
    What's wrong with it?

    Thanks
     
    peter_anderson, Jul 14, 2009 IP
  2. jbrooksuk

    jbrooksuk Active Member

    Messages:
    127
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #2
    Make sure the file has either 755 or 777 CHMOD permissions.
     
    jbrooksuk, Jul 15, 2009 IP
  3. whjtoby

    whjtoby Greenhorn

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #3
    <?php
    $file = 'people.txt';
    // The new person to add to the file
    $person = "John Smith\n";
    // Write the contents to the file, 
    // using the FILE_APPEND flag to append the content to the end of the file
    // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
    file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
    ?> 
    PHP:
    you can see file_put_contents and file_get_contents
     
    whjtoby, Jul 15, 2009 IP
  4. goscript

    goscript Prominent Member

    Messages:
    2,753
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    315
    #4
    You've got 2 mistakes:

    remove comments from this line:
    //$text = fread($file, $size); 
    PHP:
    like this
    $text = fread($file, $size); 
    PHP:
    <textarea name="editor1"><?php $file ?>></textarea>
    PHP:
    should be:
    <textarea name="editor1"><?php echo $text ?>></textarea>
    PHP:
     
    goscript, Jul 15, 2009 IP
  5. peter_anderson

    peter_anderson Notable Member

    Messages:
    3,382
    Likes Received:
    152
    Best Answers:
    0
    Trophy Points:
    240
    #5
    Thanks, it worked :)
     
    peter_anderson, Jul 15, 2009 IP