SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Fluffy Kitten Programmer~ Elledan's Avatar
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    1,356
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating and deleting lines from text file using PHP?

    I'm clueless on how to accomplish this, can you help me?

    The text file looks like this:

    name;url;content;description
    name2;url2;content2;description2

    etc.

    Can you just overwrite lines? And when deleting, can you remove the entire line, so that there' s no whitespace?

    Thanks.
    www.nyanko.ws - My web-, software- and game development company.
    www.mayaposch.com - My personal site and blog.

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you can randomly access files in PHP as you appear to want to do. If we were talking about binary files and C or C++ that would be another matter. But, the only way I know how to read a file in PHP is as a text file and sequentially. Thus you will need to read in the entire file, put it into an array structure, edit the elements you want to alter, then write everything back to file, over-writing the file content with the new content, and close the file.

    file() is a nifty function because with the one function it will open the file, read its contents as an array, with each line being an array element (note that the newline character remains attached to the end of the string in each array element).

    You can also use explode() to place your semi-colon (";") delimited values into an array, forming a two-dimensional array.

    eg:
    PHP Code:
    <?
    $filename 
    "filename.txt";
    $fileArr file($filename);
    $n count($fileArr);

    // now make $fileArr into a 2-dimensional array
    // with each element an array extracted from the delimited string
    for ($i 0$i $n$i++) {
       
    $fileArr[$i] = explode(";"$fileArr[$i]);
    }

    // do your doda here

    // now write the array back to file (overwriting the existing content)
    $fp fopen($filename"w");

    foreach (
    $fileArr AS $lineArr) {
       
    // pack each array into a tokenized string
       
    $lineArr implode(";"$lineArr);
       
    fwrite($fp$lineArr);
    }

    fclose($fp);
    ?>
    Note: it probably is good defensive practice to first create a back-up of the file you are about to overwrite.

  3. #3
    Fluffy Kitten Programmer~ Elledan's Avatar
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    1,356
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.. what I intended to do appears to be impossible, but. thanks for your help

    One more question, though: is it necessary to lock a file before reading/writing like in Perl?
    www.nyanko.ws - My web-, software- and game development company.
    www.mayaposch.com - My personal site and blog.

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Elledan
    One more question, though: is it necessary to lock a file before reading/writing like in Perl?
    yeah, if it's possible that another script will access the file at the same time. i'm not sure about this warning though in the manual:

    On most operation systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you cannot rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance!
    SOO, i don't know if that's a problem in PHP as an Apache module. i think it might be OK on *nix w/ Apache b/c it shouldn't matter if locking is done at the process level as Apache starts a new process for each request. BUT, i think locking would be a problem for me running development scripts on Win2k b/c on Win32 Apache starts a new THREAD for each request in the SAME PROCESS.

    you don't have these problems w/ Perl running through CGI (i.e. not mod_perl) b/c each Perl script always runs in its own process. although mod_perl, i think, would have the same problem.

    hope that helps anyone wondering. just something to look out for...
    Last edited by DR_LaRRY_PEpPeR; Jul 10, 2001 at 13:56.

  5. #5
    Fluffy Kitten Programmer~ Elledan's Avatar
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    1,356
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script runs on a Linux server (Redhat) and I develop it on my Linux box (SuSe) and Win2k box. I guess it should work then.

    Thanks

    BTW regarding your script, freakysid: wouldn't it be better to read the file into an array, change the content of this array, rename the file (adding .bak or something as extension) after which you create a new file and write the content of the array into that file?

    That way you'll have at least a backup if it goes wrong. Tell me if I'm saying something stupid here
    www.nyanko.ws - My web-, software- and game development company.
    www.mayaposch.com - My personal site and blog.

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Elledan, I agree that's a good idea (to rename the file as a backup and then write to a new file). That's what I hinted at in the last line of my post. I had already written my code when I realized I should have done this (as good programming practice) and was too lazy to go back and change my code

  7. #7
    Fluffy Kitten Programmer~ Elledan's Avatar
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    1,356
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol... I see. Thanks for your help
    www.nyanko.ws - My web-, software- and game development company.
    www.mayaposch.com - My personal site and blog.


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
  •