
|
View Full Version : PHP this is a toughy
kneuf 03-06-2003, 10:15 PM ok, heres my prob., say i have this file, called accounts.fil, and every user looks like this:
md5encriptedusername|md5encriptedpassword|points|so on
and I want to update the "Points Part" only of a specifec user.
BTW, im making a points system thing. Like www.neopets.com
sasha 03-06-2003, 10:54 PM If you just want to parse and update file you might wanna take a look at sample code here:
http://www.webhostingtalk.com/showthread.php?s=&postid=962909
It is the same principal. The smarter way to do that is with database.
kneuf 03-06-2003, 11:20 PM hehe, thats another 1 of my threads... i know about mySQL, just don't know how to use it... maybe i look into it tommarrow... anyway thanxs.
jd603 03-13-2003, 09:26 PM What you want to do is update only a portion of a plain text file that is separated with "|"'s?
What you should do is read the file into arrays, update the arrays and then write the file over again.
cyberlot 03-16-2003, 09:08 AM <?
$file = file('name','r+');
while (!feof ($file)) {
$buffer = fgets($file, 4096);
if($buffer needs to be changed) {
edit line and make changes $newline = $changes.
break;
}
/* Where are we reading next */
$place = ftell($file);
}
/* We just broke out, We know what line we where on from $place, lets write changes there */
reseek($file,$place);
fwrite($file,$changes);
fclose ($handle);
?>
Just threw this together, so good chance it wont work exactly but im sure you get the general idea.. This prevents you from having to rewrite the whole file so it will be a bit faster.
kneuf 03-16-2003, 11:43 AM cyberlot, could you or someone else explain the steps in the above post? where would I tell to change something?
solidphp 03-16-2003, 09:47 PM Cyberlot has good code there. If you did want to rewrite it everytime you could use this code:
// define a path to the data file
$filename="/path/to/your/file.ext";
// Read in file.
$lines=file($filename);
// Read each line of the file
foreach ($lines as $line_num => $line)
{
// because you know what|each|value|should|be|already|
list($username, $password, $points)=split("[|]", $line);
// in this example we add 1 to the old value of $points.
$points+=1;
// now we build a buffer
$buffer.=$username."|".$password."|".$points."|\n";
}
// trim the last \n off the buffer
$buffer=trim($buffer);
// rewrite the new data file
$read=fopen($filename, "w");
fwrite($read, $data);
fclose($read);
// change the file permissions
chmod($filename, 0777);
You would only have to edit the $filename, list() and write your code for point manipulation. This code should work without error.
solidphp 03-16-2003, 09:50 PM One thing to note is that the $line_num in the foreach is an ordinal count. Even though we don't use it in this example script, knowing that it is a count could be useful.
cyberlot 03-16-2003, 10:43 PM The whole point of my script was to avoid loading the whole file, then rewriting the whole file..
using ftell and reseek you keep the loading of data and the writing of data to the least possible amount.
solidphp 03-16-2003, 11:09 PM That's true, great code you have there cyberlot - much more efficient than what I posted.
My example shows how you would do it if you didn't mind reading in and rewriting the whole file. Plus, the code I posted has some pretty useful examples of reading and writing text files that could be used in other places.
Either way should work just fine. If you are storing so much data in a flat file that efficiency becomes an issue, you should move the flat file to a database.
kneuf 03-17-2003, 05:35 PM thanks for all your help, but could anyone explain whats happening in cyberlots code? where do I add the new points?
i'll post cyberlots code again:
<?
$file = file('name','r+');
while (!feof ($file)) {
$buffer = fgets($file, 4096);
if($buffer needs to be changed) {
edit line and make changes $newline = $changes.
break;
}
/* Where are we reading next */
$place = ftell($file);
}
/* We just broke out, We know what line we where on from $place, lets write changes there */
reseek($file,$place);
fwrite($file,$changes);
fclose ($handle);
?>
thanx in advance!
kneuf 03-18-2003, 05:21 PM But can anyone explin cyberlots code?
<?
$file = file('name','r+');
while (!feof ($file)) {
$buffer = fgets($file, 4096);
if($buffer needs to be changed) {
edit line and make changes $newline = $changes.
break;
}
/* Where are we reading next */
$place = ftell($file);
}
/* We just broke out, We know what line we where on from $place, lets write changes there */
reseek($file,$place);
fwrite($file,$changes);
fclose ($handle);
?>
cyansmoker 03-20-2003, 08:47 PM It would seem to me that Cyberlot's code will work only if all rows have a fixed length...otherwise, the following rows will be pretty messed up.
kneuf 03-20-2003, 09:01 PM its ok people, thanxs for all your help, but using a flatfile database is just to tough for me, so im going to do this in a database.
solidphp 03-20-2003, 09:15 PM Actually, his code wouldn't work at all if you were trying to get it to run. Just a few of the reasons:
1. reseek is not a PHP function - think he meant fseek.
2. $file = file('name','r+'); in incorrect, the second attribute actually means include, not a file permission
3. fseek won't work with files opened with file.
There is nothing wrong with rewriting the whole file. You will never know the difference in resource usage or speed.
Kneuf, the code I posted will work for you. Just define the file path, write code for your point calcs. and your ready to go!
kneuf 03-20-2003, 09:24 PM i tried your code HTTPeasy, but it just erased the file contents. Unless I did something wrong, I dont know, anyway, mySQL looks a lot easier to do this sort of thing. thanks anyway!
solidphp 03-20-2003, 09:40 PM Your right, that code won't work. The following will:
// define a path to the data file
$filename="/path/to/your/file.ext";
// Read in file.
$lines=file($filename);
// Read each line of the file
foreach ($lines as $line_num => $line)
{
// because you know what|each|value|should|be|already|
list($username, $password, $points)=split("[|]", $line);
// in this example we add 1 to the old value of $points.
$points+=1;
// now we build a buffer
$buffer.=$username."|".$password."|".$points."|\n";
}
// trim the last \n off the buffer
$buffer=trim($buffer);
// rewrite the new data file
$read=fopen($filename, "w");
fwrite($read, $buffer);
fclose($read);
// change the file permissions
chmod($filename, 0666);
The problem was with this line:
fwrite($read, $data );
It should have read:
fwrite($read, $buffer);
A database is good too!
cyberlot 03-21-2003, 08:13 AM Originally posted by HTTPeasy
Actually, his code wouldn't work at all if you were trying to get it to run. Just a few of the reasons:
1. reseek is not a PHP function - think he meant fseek.
2. $file = file('name','r+'); in incorrect, the second attribute is actually means include, not a file permission
3. fseek won't work with files opened with file.
There is nothing wrong with rewriting the whole file. You will never know the difference in resource usage or speed.
Kneuf, the code I posted will work for you. Just define the file path, write code for your point calcs. and your ready to go!
Yea couple miss types, was a bit early in the morning, the file('name','r+') should be fopen which fixes 2 and 3, 1 dont know what I was thinking but fseek is correct..
Why cyansmoker things this requires a fixed length I do not know..
fgets($file, 4096)
says to stop at 4096 chars OR at EOL/Carriage return
cyansmoker 03-21-2003, 01:50 PM Because if the new content of $changes if longer than what you previously read, this line:
fwrite($file,$changes);
will clobber the next record in your file.
cyberlot 03-22-2003, 09:23 AM Originally posted by cyansmoker
Because if the new content of $changes if longer than what you previously read, this line:
fwrite($file,$changes);
will clobber the next record in your file.
Not if your placing your content in there each on its own line which is what I assumed he was doing..
klynn 03-22-2003, 10:55 AM >>// change the file permissions
>>chmod($filename, 0666);
Please remember to always choose the least amount of permissions in order to accomplish the task. These permissions leave the file capable of being written to by other users on the system.
cyberlot 03-28-2003, 10:27 AM Because this is php, your sort of stuck on that issue, All files created are owned by the webserver and if you dont give them those permissions you will be unable to delete them by hand if you need to..
|