#46
  1. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: Apend to beginning of file.



    <!-- Code -->
    <p><pre><font color=#008000><xmp>
    Sorry, my first post got crunched. This is what it should have said:

    Is there an append mode that starts at the BEGINNING of a file? r+ will write over anything that's already there. For instance, if I have the file:
    <file>
    All that glitters is gold
    And she's buying a stairway to heaven.
    </file>

    and I open the file with r+, and 'add' the text:
    <text>
    There's a lady who's sure
    </text>

    Then the resulting file is:
    <file>
    There's a lady who's sure
    And she's buying a stairway to heaven.
    </file>

    But I want:
    <file>
    There's a lady who's sure
    All that glitters is gold
    And she's buying a stairway to heaven.
    </file>

    Was this clear? Anybody have any ideas?
    </xmp></font></pre><p>
    <!-- Code -->

  2. #47
  3. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    all one line??


    I really needed this tutorial and it was great. I am having one problem. I have a text entry area that collects text kind of like a simple bulliten board and then posts it to the bottom of the page. If the user does not hit the return key, I don't get a new line. All the messages run together.
    <br>
    <br>
    see http://www.time4mom.com/imfamily/abby.php
    <br>
    <br>
    How can I fix this?
  4. #48
  5. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: all one line??


    I think this might help. I altered the last example in the article to add html line breaks after each entry. note that I also changed the variables in the article a bit


    <!-- Code -->
    <p><pre><font color=#008000><xmp>
    <html>
    <head>
    <basefont face="Arial">
    </head>

    <body>

    <?php

    //set file name
    $story = "mystory.dat";

    //open it
    $handle = fopen($story, 'a+');

    //add a br
    $graffiti .= "<br>";


    //write string to file handle
    fputs($handle, $graffiti);

    //close the file
    fclose($handle);

    ?>

    <form action=story.php3 method=get>
    <input type=text size=30 name=graffiti>
    <input type=submit name=submit value="Add your two bits!">
    </form>

    <?php
    //display file if present
    if(file_exists($story))
    {
    echo "<b>Current graffiti reads: </b>";
    readfile($story);
    }
    else
    {
    echo "file not found";
    }

    ?>
    </xmp></font></pre><p>
    <!-- Code -->


    -jimmy
  6. #49
  7. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    so what am I doing wrong?


    As a novice, and having read the full tutorial, I'm trying to read a text file which has colon-delimited data
    <br>
    <br>
    eg
    <br>
    <br>
    f1:f2:f3:f4:f5:f6
    <br>
    <br>
    I'm using the following to get each line of the file into an array:
    <br>
    <br>
    // read file into $res array
    <br>
    $result = file($filename) ;
    <br>
    <br>
    I'm then trying to read each line, and get the (6) separate fields into another array:
    <br>
    <br>
    // Load each result into $team array
    <br>
    for ($c1=0; $c1<6; $c1++)
    <br>
    *****{
    <br>
    *****$resstr = $result($c1);
    <br>
    *****$team = explode(":", $resstr);
    <br>
    };
    <br>
    <br>
    This gives an error
    <br>
    "Function Names must be Strings"
    <br>
    <br>
    I can't see what's wrong - can anyone more experienced point out the error of my ways please?
    <br>
    <br>
    Many thanks,
    <br>
    Alan S>
    <br>
  8. #50
  9. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: so what am I doing wrong?


    Doh! Answering my own post:

    Array members go in square brackets!

    $array[n] not $array(n)

    Fool!!
  10. #51
  11. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    directory listing


    Anyone out there know if there is a php command for listing all the files of a directory? or perhaps importing them into an array?
  12. #52
  13. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: Apend to beginning of file.


    Hi dusty,

    there is always a couple of ways of doing things, as for why it doesnt work, not sure!

    but this should work,

    $string = "There's a lady who's sure";
    // set string

    $file = join('',file("filename.txt"));
    // Get all info from txt file

    $fp = fopen ("filename.txt", "w");
    // open file to Overwirte
    fwrite($fp, "$string\n");
    // write in new string
    fwrite($fp, "$file\n");
    // write in orginal text
    fclose ($fp);


    hope this helps,

    Robert
  14. #53
  15. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: Apend to beginning of file.


    Thanks, this is exactly what I was trying to do. And it's a lot nicer than the sloppy 15 lines or so I had before (array). Thank you very much.
    <br>
  16. #54
  17. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Quite easy, but there is a little "bug" while inserting a string...


    All the examples are good and perfectly working, but there is a little thing to notice that is causing me some troubles:
    <br>
    <br>
    if you use the PHP function "fputs()" or "fwrite()" using "fopen" with a+ mode you can easily add strings to a file.
    <br>
    <br>
    The problem is: what if you want add the string without go in a new line ?
    <br>
    <br>
    Yes, because if you want append a string at end of an existing row it seems not possible because the functions adds string to a new line.
    <br>
    <br>
    Is there a way to fix this ?
    <br>
    <br>
    Thank you
    <br>
    <br>
    Fabio
  18. #55
  19. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: php files


    This message maybe overdue....

    Try wordpad: c:\windows\write.exe
  20. #56
  21. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Post vs Get methods


    I've been wondering about this for a while now, what is the difference between passing variables using a GET or a POST ? Using GET, they appear in the url, which could be undesirable, but using POST, the user can't reload the page. I'd like some input security-wise also, thanks...
  22. #57
  23. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Random number isn't random...


    <html>
    <br>
    <P>In the tutorial, the author used the <i> rand(2, 100)</i> function, which
    <br>
    needs a seed value (according to <a href="http://www.php.net/manual/function.rand.php">http://www.php.net/manual/function.rand.php</a>).
    <br>
    Otherwise, the $wonderland remains constant at the value 2. </p>
    <br>
    <p>*</p>
    <br>
    <p>PS: You might want to consider the function mt_rand( min , max ), which
    <br>
    generates (apparently) a better random number. It also needs a seed, which you
    <br>
    can calculate from the system clock like this:</p>
    <br>
    <p>mt_srand ((double) microtime() * 1000000);</p>
    <br>
    </html>
    <br>
  24. #58
  25. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)
    Okay, so I understand how to display the contents of a text file, no problem.
    <br>

    <br>
    But, what if I need to be able to format the contents of that file? For instance... ;)
    <br>

    <br>
    The file's contents are dilineated into various sections by a unique string of characters. What I'd like to do is format the text in between the 'unique string of characters' into a table... any ideas?
    <br>

    <br>
    Thanks a lot :)
    <br>
    John
  26. #59
  27. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: Post vs Get methods


    Well it's really a toss up between what you would like to do. If you don't want a URL they can reference again, for example with a form-to-email script, use POST. If you need the URL, use GET. However if you are using a textbox in the input form, using the GET method may be undesirble unless you want a ridiculously long URL that Could (I'm not sure of actual maximum lengths) be truncated.

    Either way, PHP will get the data. I recommend using get for forums, database queries, etc. and I would probably use POST for user login forms and such, so that their password wouldn't be displayed in the URL.
  28. #60
  29. No Profile Picture
    guest
    Guest
    Devshed Newbie (0 - 499 posts)

    Re: PHP 101 is right


    Randall: please take your childish remarks elsewhere. This has been a very helpful tutorial for many including myself, I just migrated from Perl/CGI. If you're beyond the "101" level then go find advanced tutorials and quit complaining about this tut being "lame".

    It also makes no different what you name the extension for christ's sake. Call it index.php238293, index.peehpee if you want, just tell apache to look for that extension.

IMN logo majestic logo threadwatch logo seochat tools logo