Results 1 to 8 of 8

Thread: Issues with fwrite in php

  1. #1
    Join Date
    Nov 2004
    Location
    NYS
    Posts
    29

    Default Issues with fwrite in php

    I have a .txt based database setup, and when I do fscanf to find a uname, then fseek to go right after it, then fwrite it fwrite's over the \n, and over the top of the rest of the unames... I cant figgure out how to stop this overwriting in the file. My script is

    <html>
    <body>
    <?php

    if(!($_SERVER['HTTP_REFERER']==='http://dburg-php.bizhat.com/teacher-consol.php'))
    exit('You can not access this page. Please log into your console and edit the database that way.');

    switch($_POST[f_type]) {

    case school:



    echo "school right
    ";
    if(!($s=fopen('sdb.txt','r+')))
    exit('Unable to open schoolwork database file.');
    fscanf($s,$_COOKIE['uname']);
    fseek($s,-3,SEEK_CUR);
    fwrite($s,"|$_POST[month]-$_POST[day]");
    fwrite($s,"|$_POST[topic]");
    fwrite($s,"|$_POST[subject]|\n");
    echo(ord(fgetc($s)));




    /* while(!(ord(fgetc($s))==='254'))
    {
    fseek($s,-1,SEEK_CUR);
    fwrite($s,'');
    }
    */ break;
    case home:
    echo 'home right';
    if(!($h=fopen('hdb.txt','r+')))
    exit('Unable to open homework database file.');
    break;
    default:
    echo 'This page was not retrieved correctly. Please re-log into your console and try again.';
    break;
    }


    ?>
    </body>
    </html>

    It's been commented out in a few spots and others aren't completed

    but it's the basic function under the school case. Any ideas? ty in advance

  2. #2
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    You need to chmod the file to 777 then only script able to write the file.

    When posting, always give the URL, so others can look and see what is the error you are getting.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  3. #3
    Join Date
    Nov 2004
    Location
    United Kingdom
    Posts
    13

    Default

    take off the '+' from 'r+' 'r+' reads and writes the file I think, if it has a plus after it reads and overwrites the files

  4. #4
    Join Date
    Dec 2004
    Posts
    12

    Default

    give us the that page url..
    and i will give ur answer..
    and u must 777 chmod php file..
    and check those files exist on ur site ..'sdb.txt' and 'hdb.txt'
    if exist , make chmod 777

  5. #5
    Join Date
    Nov 2004
    Location
    NYS
    Posts
    29

    Default

    chmod isn't my problem, I set access to my files manually in the php. The files CAN be written to.

    I figgured out the problem. PHP does not have any function capable of inserting a string into a file. It overwrites everything.

    I'm writing a function to solve this problem. I'll post it when I am done for anyone with this problem in the future.

  6. #6
    Join Date
    Dec 2004
    Posts
    10

    Default

    did you try to use fputs?

  7. #7

    Default

    u will have to use a logic like this.
    jump to the end.
    while (record loc > place where u want to insert text)
    {
    go back 1 record
    read it.
    write it just after the read record.
    go back 2 records.
    }
    write the new record.

  8. #8
    Join Date
    Dec 2004
    Posts
    20

    Default

    if all else fails, load the entire file into an array, replace what you need to replace, then write the file.

    try something like this:
    $file = file("./file.txt");
    $s = fopen("./file.txt","w");
    foreach($file as $line)
    {
    $line_arr = explode("|",$line);
    if($line_arr[0] == $_COOKIE[uname]){fwrite($s,"$_COOKIE[uname]|$_POST[month]-$_POST[day]|$_POST[topic]|$_POST[subject]|\n");}
    else{fwrite($s,$line);}

    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •