Web Hosting Talk







View Full Version : PHP question (I'm stumped)


EAS Chris
05-07-2005, 12:13 AM
I've asked a few programmers this question and nobody seems to know the answer... I've finally resorted to asking at a message board (oh the shame! lol)

Ok, let's say I have a list of things in an html file


Item 1<br>
Item 2<br>
Item 3<br>
Item 4<br>
Item 5<br>


What I want is a PHP script where I can enter an item name (say item 3) and have the script delete *only* that specific content from the file.

So, how do I write a script that opens up the file, finds the specified text and deletes that text? Is it even possible?

azizny
05-07-2005, 12:18 AM
if its the same format as above:

$f = fopen('filename','r');
$text = fread($f,filesize('filename'));
fclose($f);

$break_it = explode("<br>",$text);

foreach($break_it as $line){
if($line != $item_toberemoved){
$new_file .=$line.'<br>';
}
}

$f2 = fopen('filename','w+');
fwrite($f2,$new_file);
fclose($f2);

What we are actually doing is copying the file, removing the item we dont need and then making the file again..

Peace,

EAS Chris
05-07-2005, 04:00 AM
Hmmm... nice script, but I don't think it'll work. My fault, I should've mentioned that the file with the list is being updated a lot. Think of a list of users in a chat room (that's not what it is, but that example will do nicely). As people log in and out the list constantly changes. The list I want needs to do the same (only based on a variable being passed through a form, not based on sessions). Writing to the list to add items is easy, but I'm having a heck of a time figuring out how to remove the items from the list on command.

EAS Chris
05-07-2005, 04:10 AM
Ok, a little more detail for you. Here's the relevant portion of the code:


if ($mode == 'additem') {
$list=fopen("list.php", "a");
fwrite($list, "$item<br>");
} elseif ($mode == 'delitem') {
***SCRIPT THAT I NEED GOES HERE***

error404
05-07-2005, 04:50 AM
Make absolutely sure you wrap that code in the appropriate flock() (http://ca.php.net/flock) calls if you're expecting concurrent access. You'll likely get data corruption (or more likely, you'll lose a submission) eventually if you don't.

The basic algorithm behind doing something like this is:
1) Read the entire file into memory
2) Modify it in-memory to remove the part you don't want (how this would be done is up to you, it should be reasonably simple, but I hate doing text processing so I won't do it for you)
3) Write the entire file out again

I must ask though, while I don't think databases are the answer to everything, this seems to be a prime candidate for a database-based solution. Why aren't you using one? The text-based code will be more bug-prone and a lot more work.

Burhan
05-07-2005, 05:14 AM
You can just do this (if you have access to sed):


<?php exec("sed -e '/Item 3/d' < somefile.txt"); ?>

hiryuu
05-07-2005, 06:35 AM
SQL would be a good choice for the workload your describing. Even if you stick to flat files, I suggest using a file format that is convenient for the add/remove activity, rather than trying to retrofit the code to handle the format. If you wish, you can still have the code generate a static HTML file, but you would need a LOT of traffic to notice a difference over generating it on the fly.

EAS Chris
05-07-2005, 05:50 PM
Thanks for the suggestions, guys.

fyrestrtr, that would work wonderfully if I had access to SED...

error404 and hiryuu, I agree that using a database would probably make this a whole lot easier. Unfortuneately, I am a newbie at PHP. Still learning the ropes and I have not yet learned how to deal with databases. I can always upgrade this script to work better once I understand DBs, but for now I just wanted to have something that would work.

I'm guessing that since none of the suggestions thus far have worked, then there probably isn't any way to do this without using a DB. Oh well, it was worth a shot.

A very big thank you to everyone who responded in this thread. I really appreciate your help.

intransit
05-08-2005, 12:06 AM
Er, didn't test my code enough. I'm looking at a solution for you, though. I'll post it if it actually works next time..heh.

intransit
05-08-2005, 12:30 AM
Ok, I tried to optimize it and ended up just screwing it up. :( But anyways, this should do what you need. As far as I know, the only way to edit a flat file is to write it back over itself. Even if you are doing this many times it wont be a concern as far as speed goes, unless the file is thousands of lines long, at which point a database would be a better solution. This is somewhat similar to what azizny wrote, but if you have any problems with it pm me.


<?php

//test variables
$item="Item 2";
$mode='delitem';

if ($mode == 'additem') {
$list=fopen("list.php", "a");
fwrite($list, "$item<br>\r\n");
fclose($list);
} elseif ($mode == 'delitem') {
//***SCRIPT THAT I NEED GOES HERE***

//load file into an array
$list=file("list.php");
for($i=0;$i<count($list);$i++){

//remove <br>, trim string
$list[$i]=str_replace('<br>',null,trim($list[$i]));

//delete desired item
if($list[$i]==$item){$list[$i]=null;}
}

//open file for writing, truncated
$fp=fopen("list.php", "w");
foreach($list as $line){

//if it's not the line we deleted, write it back into the file
if($line){fwrite($fp,$line."<br>\r\n");}}
fclose($fp);
}
?>


Good luck.

gogocode
05-09-2005, 05:35 AM
Don't forget to add locking to that script!

sasha
05-09-2005, 07:59 AM
Why not just create folder and have one file in it representing each line of current text file. Then you would just have to create and unlink files and wonld not have to worry about data corruption and locking too much.