Results 1 to 12 of 12

Thread: Read File

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    16

    Read File

    How can I read whats in a text file and display it on the page. Say the text file looks like:
    This
    is
    a
    test
    of
    the
    file

    How can I display it on the website? Also is there a way i can delete a certain row such as the word test so the text file would look like:
    like:
    This
    is
    a
    of
    the
    file

    Thanks

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Read File

    I dont know how to delete a word, but id presume youd use an if statement with file()..but anyways this will read a file
    PHP Code:
    $filename "yay.txt";
    $Handle fopen($filename"r") or die("error opening");
    $Contents =fread($handle,filesize($filename));
    fclose($handle);
    echo(
    $contents); 

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    16

    Re: Read File

    Warning: fread(): supplied argument is not a valid stream resource in /home/mcjedi/public_html/test.php on line 4

    Warning: fclose(): supplied argument is not a valid stream resource in /home/mcjedi/public_html/test.php on line 5

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Read File

    my bad

    PHP Code:
    <?php
    $filename 
    "visitorcount.txt";
    $Handle fopen($filename"r") or die("error opening");
    $Contents = @fread($Handle,filesize($filename));
    @
    fclose($Handle);
    echo(
    $Contents); 
    ?>

  5. #5
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: Read File

    try this:
    NOTE: this is untested!
    PHP Code:
    <?php if($_POST['B1']){
    $filedata file('file.txt');
    $i 0;
    foreach (
    $filedata as $line_num => $linedata) {
      if(
    $_POST["C$line_num"] == 0){
        
    $filereturn[$i] = $linedata;
        
    $i++;
      }
    }
    $handle fopen("file.txt"'a') || die("cannot write to file");
    fwrite($handleimplode("\n"$filereturn)) || die("another problem writing to file");
    fclose($handle);?><body>
    <form method="POST" action="?>echo $_SERVER['PHP_SELF']; ?>">
    <? $filedata = file('file.txt');
    foreach ($filedata as $line_num => $linedata) {
       echo htmlspecialchars($linedata) . '<input type="checkbox" name="C' . $line_num . '" value="ON"><br />' . "\n";
    }?><input type="submit" value="Delete" name="B1"></form>
    </body>
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    16

    Re: Read File

    Parse error: parse error, unexpected $ in /home/mcjedi/public_html/test.php on line 19

    ^^ ALLs, code
    Last edited by McJedi; Jul 4th, 2005 at 08:38 PM.

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Read File

    Code:
    ?>echo $_SERVER['PHP_SELF']; ?>>
    i think the ? and the > got twisted. .. but I didn't test the whole thing.. just looked it over. perhaps i'm wrong.

  8. #8
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: Read File

    ya, that was one error, but i also forgot about a closing brackett... this "should" work:
    PHP Code:
    <?php if($_POST['B1']){ 
      
    $filedata file('file.txt'); 
      
    $i 0
      foreach (
    $filedata as $line_num => $linedata) { 
        if(
    $_POST["C$line_num"] == 0){ 
          
    $filereturn[$i] = $linedata
          
    $i++; 
        } 
      } 
    $handle fopen("file.txt"'a') || die("cannot write to file"); 
    fwrite($handleimplode("\n"$filereturn)) || die("another problem writing to file"); 
    fclose($handle);
    }
    ?><body> 
    <form method="POST" action="<?echo $_SERVER['PHP_SELF']; ?>"> 
    <? $filedata = file('file.txt'); 
    foreach ($filedata as $line_num => $linedata) { 
       echo htmlspecialchars($linedata) . '<input type="checkbox" name="C' . $line_num . '" value="ON"><br />' . "\n"; 
    }?><input type="submit" value="Delete" name="B1"></form> 
    </body>
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    16

    Re: Read File

    Ok it brings up check boxes with whats in the file and has a delete button, but when you check what you want to delete and hit delete you get this:

    Warning: fwrite(): supplied argument is not a valid stream resource in /home/mcjedi/public_html/test.php on line 11
    another problem writing to file

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Read File

    You can use the file() function to load each line of the file into an array. To remove a particular line unset the array element corresponding to that line.
    PHP Code:
    <?php

        $file 
    'test.txt';

        
    /* the file function loads each line of the file into an indexed array */
        
    $file_array file($file);

        
    /* each line of the file is represented by an array element line 1 is index 0 */
        
    unset($file_array[2]); // removes line 3
    ?>
    <html>
        <head>
            <title>File Dump</title>
        </head>
        <body>
    <?php foreach ($file_array as $line): // output the file by looping through the array ?>    
            <p><?php echo (htmlspecialchars($line)) ?></p>
    <?php endforeach; ?>
        </body>
    </html>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Read File

    Extending the above example you can adelete selected lines from the file:
    PHP Code:
    <?php

        $file 
    'test.txt';

        
    /* the file function loads each line of the file into an indexed array */
        
    $file_array file($file);


        if (isset(
    $_POST['delete'])) { // if the user pressed the submit button this variable will be set
            
    if (isset($_POST['lines']) && is_array($_POST['lines'])) { // the variable is an arrway if the users selected lines to delete

                /* attempt to open file for writing - we will overwrite the entire file
                    with only the lines the user has not selected to deelte */
                
    if (! $fhwnd fopen($file'wb')) {
                    die(
    'Cannot open file for writing.');
                }

                
    /* go through each line and rewrtite it to the file only if
                   it is not present in the lines array */ 
                
    foreach ($file_array as $number => $line) {
                    if (! 
    in_array($number$_POST['lines'])) {
                        
    fputs($fhwnd$line);
                    }
                }

                
    /* save and close the file */
                
    fclose($fhwnd);

                
    /* get the new file array to display */
                
    $file_array file($file);
            }
        }   
    ?>
    <html>
        <head>
            <title>File Dump</title>
        </head>
        <body>
            <form method="post" action="<?php echo($_SERVER['PHP_SELF']) ?>">
    <?php foreach ($file_array as $number => $line): // output the file by looping through the array ?>    
                <p><input id="line<?php echo($number?>"
                          type="checkbox" 
                          name="lines[]" 
                          value="<?php echo($number?>" />
                    <label for="line<?php echo($number?>"><?php echo (htmlspecialchars($line)) ?></label>
                </p>
    <?php endforeach; ?>
                <p><input type="submit" name="delete" value="Delete Select Lines" /></p>
            </form>
        </body>
    </html>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  12. #12
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: Read File

    similer to my example, that i couldn't get all the bugs out, because i couldn't test it
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

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