Development Edit/View text file via web

Discussion in 'Software' started by Lazlow, 31 Oct 2006.

  1. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    Hopefully the thread title makes sense, I'll explain what I want to be able to do:

    I currently use a .txt file stored in my Start Menu on various computers, that I have to manually sync with each other. The .txt file is basically a to-do list, along with links to various websites I find but want to spend more time looking at later on.

    I would like it if I could view the text file via the web instead, edit it as I see fit and 'save' it too. Is this at all possible? All within a standard browser interface, Javascript, PHP, MySQL etc. It just needs to be really simple and display a single text file. Any ideas?
     
  2. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    Standard PHP file I/O will work, just read it into a buffer, display the buffer in a html text box on a form, when the submit button is pressed just write to the file.
     
  3. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    [begs]Could you give me a few pointers - or a link to some code on the web? My php is non-existant![/begs]

    EDIT: I found this. Reading through it at the moment, but not entirely sure if it's what you mean.
     
  4. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    OK, using the link in my post above, I have managed to get a form to save data to a .txt file. How do I call it when the form is first displayed, allowing it to be edited and re-saved?

    Code for index.html:
    Code:
    <form action="savedata.php" method="post">
    Text: <textarea name="text" cols="256"></textarea>
    <input type="submit" value="Save Data >>">
    </form>
    Code for savedata.php:
    Code:
    <?php
    
    $text = @$_POST["text"];
    
    // Set the string to be written to the file
    $values = "To Do: $text\r\n";
    
    // Open the file for truncated writing
    $fp = @fopen("file.txt", "w") or die("Couldn't open file.txt for writing!");
    $numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");
    
    @fclose($fp);
    echo "Wrote $numBytes bytes to file.txt successfully!";
    
    ?> 
     
  5. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    Any ideas anyone? Just needing to call the .txt file when index.html is opened - within the form itself.
     
  6. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    PHP:
    <?
    // Open the file for reading

    $fp = @fopen("file.txt""r") or die("Couldn't open file.txt for Reading!");
    $contents= @fread($fpfilesize("file.txt"));) or die("Couldn't read values from file!");
    ?>

    <form action="savedata.php" method="post">
    Text: <textarea name="text" cols="256"><? echo $contents; ?></textarea>
    <input type="submit" value="Save Data >>">
    </form>

     
    Last edited: 1 Nov 2006
  7. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    I can see what it should be doing, but it's displaying
    PHP:
    <? echo $contents ?>
    In the form, instead of the contents of the text file. Am I doing something retarded?
     
  8. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    my bad, you need to stick a semicolon after contents.

    Post edited
     
  9. Atomic

    Atomic Gerwaff

    Joined:
    6 May 2002
    Posts:
    9,646
    Likes Received:
    94
  10. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    Sorry Doug, it's still displaying the echo message and not the data. Here's my index.html:
    PHP:
    <body>
    <?
    $fp = @fopen("tasks.txt", "r") or die("Couldn't open tasks.txt for Reading!");
    $contents= @fread($fp, filesize("tasks.txt"));) or die("Couldn't read values from file!");
    ?> 
    <div align="center">
    <form action="savedata.php" method="post">
    <textarea name="text" cols="150" rows="30"><? echo $contents; ?></textarea>
    <br />
    <br />
    <input type="submit" value="Save Data &gt;&gt;" />
    </form>
    </div>
    </body>
    And here's savedata.php:
    PHP:
    <?
    header("Location: http://myurlhere/");

    $text = @$_POST["text"];

    // Set the string to be written to the file
    $values "$text\r\n";

    // Open the file for truncated writing
    $fp = @fopen("tasks.txt""w") or die("Couldn't open tasks.txt for writing!");
    $numBytes = @fwrite($fp$values) or die("Couldn't write values to file!");
    ?> 
    What am I doing wrong?
     
  11. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    That's kinda cool, a glorified version of what I'm aiming for I guess. I'm annoyed by having emails and to do lists and bookmarks scattered across several PCs/Laptops, that I'm consolidating it all on a website.
     
  12. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    First index.html should be index.php, and I was stupid, didn't fully type check.

    PHP:
    <html>
    <body> 

    <?
    if(@$_POST["Save"])
    {
        $text = @$_POST["text"]; 
        
        // Set the string to be written to the file 
        $values = "test2 $text \r\n"; 
        
        // Open the file for truncated writing 
        if(!$fp2)
        {
            $fp2 = fopen("tasks.txt", "w+") or die("Couldn't open tasks.txt for writing!"); 
        }
        else
        {
            echo "File open";
        }
        $numBytes = fwrite($fp2, $values) or die("Couldn't write values to file!"); 
        fclose($fp2);
        $_POST = array();
    }

    $fp = @fopen("tasks.txt", "r") or die("Couldn't open tasks.txt for Reading!"); 
    $contents= @fread($fp, filesize("tasks.txt")) or die("Couldn't read values from file!"); 
    fclose($fp);
    ?> 
    <div align="center"> 
    <form action="test.php" method="post"> 
    <textarea name="text" cols="150" rows="30"><? echo $contents; ?></textarea> 
    <br /> 
    <br /> 
    <input type="submit" value="Save Data &gt;&gt;" name="Save" /> 
    </form> 
    </div> 
    </body> 
    </html>
    That should work, I'm having some fwrite errors here, but I think thats a server problem.:

    EDIT:

    Changed the fopen for writing to w+
     
  13. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    It works perfectly Doug! How can I repay you?

    I think I'll look into php more and teach it to myself, as I'm coming across many things that would work well using it.
     

Share This Page