PDA

View Full Version : php


Immortal Wombat
12-06-2004, 22:48:43
I need to be able to delete bits from a text file.

In theory this should be easy, there's dozens of ways if could work, but I can't figure out how to actually make it work. I should be able to pick apart a news-posting script but that doesn't help.

:(

Sir Penguin
13-06-2004, 08:03:56
Which bits?

SP

Nav
13-06-2004, 11:30:58
without knowing what exactly you want to do. It would be better of thinking of it as loading the text file into php (ie each line into an array), deleting the lines you don't want, then rewriting the file.

if it's complicated you should really use a database.

Immortal Wombat
13-06-2004, 16:27:45
It would be better of thinking of it as loading the text file into php (ie each line into an array), deleting the lines you don't want, then rewriting the file.

Yes that's it precisely, only I can't seem to rewrite the file, only append to it. I'm using the text files as a database because it looks easier than learning databases.

Sir Penguin
13-06-2004, 19:02:27
If you're loading them into arrays, you can probably turn the array into an AVL tree or something to simulate a database's speed. :)

If PHP follows C, then there are two different relevat modes for opening a file for writing. One is write, 'w', and the other is append, 'a'. The 'w' mode overwrites the file. Beyond that, I probably don't know anything useful. Anyway, check the modes you're using to open files.

SP

Nav@work
14-06-2004, 08:46:26
Originally posted by Immortal Wombat
Yes that's it precisely, only I can't seem to rewrite the file, only append to it. I'm using the text files as a database because it looks easier than learning databases. try deleting then re-saving the file? I'm not sure if that's the correct method for text based dbs however.

Its well worth learning databases, MySQL is dead easy (hey Ive got a job doing it!), it's a lot easier to work with the data and faster too.

Sir Penguin
14-06-2004, 08:57:01
MySQL is very easy to use, but on the other hand it's another complex server you have to run on your computer, is a security liability, and it's not a whole lot faster than small text files. ;)

SP

Immortal Wombat
14-06-2004, 21:04:04
Originally posted by Nav@work
try deleting then re-saving the file? I'm not sure if that's the correct method for text based dbs however.
I get to the deleting the file bit, and it all goes tits up. I will get on to databases eventually, but I only have one to play with, so I want to save it for something that I absolutely can't use workarounds for.

Darkstar
14-06-2004, 21:39:32
Er... you cannot write back to the file while you have it open, unless you open it in the correct modes. You might be also hitting against a block/file lock, preventing you from writing back to the start of the file.

First off, try writing you results to a NEW file. Just to make sure you got the writing bit correct. If so, I'd suggest you look into making sure you've closed and release all file resources you used when you opened the file for read, then try opening the file in write mode, and dumping you results back over the top of your file... if you don't want to do the old file shuffle. (File shuffle... read in your data, manipuate it as desired in memory, rename input file with .old or whatever, open up new file under original name.)

A few suggestions for you.

Darkstar
14-06-2004, 21:50:36
Oh... in C, you'd need to open the file in "rw" (read/write) mode and not "ra" (read/append) mode. I don't know the php equivs, but its all got to do with opening the file in the correct mode for your access needs.

Immortal Wombat
14-06-2004, 22:02:21
Originally posted by Sir Penguin

If PHP follows C, then there are two different relevat modes for opening a file for writing. One is write, 'w', and the other is append, 'a'. The 'w' mode overwrites the file. Beyond that, I probably don't know anything useful. Anyway, check the modes you're using to open files.

I was using 'a'. Oops. Anyway. Hooray. That should make things easier.

Thanks guys.

LoD
14-06-2004, 22:25:06
For the future, you'll also need to make sure that PHP has the necessary permission to operate on the relevant files (esp. on Linux systems).

Immortal Wombat
15-06-2004, 17:43:58
Yes yes. I can do writing. I just couldn't do deleting.

Nav
20-06-2004, 12:21:27
http://www.php.net/manual/en/function.unlink.php

remember you need to have the full path to the file and the filename.