A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: I'm a PHP noob

  1. #1
    Member
    Join Date
    Aug 2001
    Posts
    50

    I'm a PHP noob

    so i'm sure one can answer my question very quickly, i'm extremely new to php, but with what i know of it so far i'm trying to create an html form to write to a text file via php. very simple, all it needs to write is what is typed into the form and the date. so with what i know of php i tried to create a script for the data to pass through to the file, but i'm failing miserably. heres what i've come up with so far.

    i'm noob to the uber maximus so please dont laugh =/

    <?
    function writenews($file) {

    $dateStamp = strftime("%D %T", time());
    $file = fopen("news.txt", "w+");

    fwrite($file, "$dateStamp|$input\n");
    fclose($file);

    success();
    }
    ?>

    if you could help me, or supply me with a simply script to write to a txt file you would be awesome and i would love you. tyi

    you say insanity like its a BAD thing

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    this snippet may be good code, but it certainly is not by itself... you are defining a function and calling another one.
    If you want to write a file in response to some flash movie (or html form) sending data, the code should be (for old php versions):
    Code:
    <?
    $dateStamp = strftime("%D %T", time());
    $file = fopen("news.txt", "w") or die("&status=cannot");
    
    fwrite($file, "$dateStamp|$input\n");
    fclose($file);
    print "&status=ok";
    ?>
    or - for new php versions
    Code:
    <?
    $dateStamp = strftime("%D %T", time());
    $file = fopen("news.txt", "w") or die("&status=cannot");
    $input = $_POST["input"];
    
    fwrite($file, "$dateStamp|$input\n");
    fclose($file);
    print "&status=ok";
    ?>
    The latter one assumes that your movie is sending POST method.
    The print line is not strictly necessary - however if you have a "status" textfield on stage, it would show a reassuring message
    The w+ access mode means write, then read, which seems a bit unusual for a news app. Did you mean a (add to the end) instead?
    The die parts should give you feedback if you cannot write to text file, due to lack of permissions - on unix server you should chmod the text file to 666

    Musicman

  3. #3
    Member
    Join Date
    Aug 2001
    Posts
    50
    thanks very much, yes you were right i should have used "a" instead of "w+"

    but another thing. how would i go about having the input write itself to the top of the file, rather than the end? (as this is a news file, i need the most recent entry to go to the top) thank you so much for your help =)

    my script currently looks like this:

    <?
    $dateStamp = strftime("%D %T", time());
    $file = fopen("news.txt", "a") or die("&status=cannot");
    $input = $_POST["input"];
    fwrite($file, "$dateStamp\n $input\n");
    fclose($file);
    print "&status=ok";
    ?>

    you say insanity like its a BAD thing

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    there is no simple "writo to top" function. Basically you have two options, and the first one is more suited for a news script - read existing content, then save new and old messages
    Code:
    <?
    $dateStamp = strftime("%D %T", time());
    $file = fopen("news.txt", "r+") or die("&status=cannot");
    // flock($file, LOCK_EX);
    $exist = "";
    while($data = fgets($fp, 1024))
     $exist .= $data;
    $input = $_POST["input"];
    // start from top
    fseek($file, 0, SEEK_SET);
    fwrite($file, "$dateStamp\n$input\n");
    fputs($filem $exist);
    // flock($file, LOCK_UN);
    fclose($file);
    print "&status=ok";
    ?>
    Note: this code opens the file once and only closes it after the changes are complete. There is also some commented locking code which might make sense for a guestbook type application

    The second approach would still add messages at the end but use another script to reverse the lines when reading. While this might look like extra effort, it automatically solves the caching problem (visitors seeing old news after you added new text)
    Also there are a few considerations for files in shared access: consider again the guestbook - two visitors might add an entry at about the same time, and an operator might be trying to remove an unwanted posting

    Musicman

  5. #5
    Member
    Join Date
    Aug 2001
    Posts
    50
    first of all i want to thank you very much for your assistance, its been a huge help. my only problem remaining: at the beginning of the document i need to keep the text "&news=" so that flash can load it through "loadVariables". if i keep the value in fseek to 0, then the text gets loaded before &news=, and if i set it to 6 (the amount of characters in &news=) then it starts overwriting prexisting entries. is there any way around this?

    Hopefully this is the last time i'll have to bug you

    you say insanity like its a BAD thing

  6. #6
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    not tested...
    Code:
    <?
    $dateStamp = strftime("%D %T", time());
    $file = fopen("news.txt", "r+") or die("&status=cannot");
    // flock($file, LOCK_EX);
    $prefix = fread($file, 6);
    $exist = "";
    while($data = fgets($fp, 1024))
     $exist .= $data;
    $input = $_POST["input"];
    // start from top
    fseek($file, 0, SEEK_SET);
    fwrite($file, "&news=");
    fwrite($file, "$dateStamp\n$input\n");
    fputs($filem $exist);
    // flock($file, LOCK_UN);
    fclose($file);
    print "&status=ok";
    ?>
    Musicman

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

HTML5 Development Center