PDA

View Full Version : PHP: command to add info to top of file


Bouley86
02-04-2003, 06:57 AM
Alrite, I have a script that will write things to a file for a small, very basic guestbook. But, they add the new entries to the bottom of the list and I want them added to the top. What do I have to put into the write command that makes the script add the new entry to the top of the file?

scoutt
02-04-2003, 09:14 AM
http://www.snippetlibrary.com/viewhtml.php?id=6&kid=14&siteid=234

Bouley86
02-04-2003, 03:26 PM
Thanks..but I've added that code and changed the things i needed to..but it still doesnt write to the top of the file. Here's what I'm workin with:

switch ($option2)
{

case 'sign':
echo("<center>Thanks for signing my guest book! You can now view it in my profile!</center>");


$file_name = "$dir/$sn/gbook.txt";
$fp=fopen("$file_name", "a+");
flock($fp, 2);
$old_data = fread($fp, ($file_name));
rewind($fp);
fwrite($fp, "<a href=\"aim:goim?ScreenName=$who\">$who</a>: $gbook<br>\n" . $old_data);
flock($fp,3);
fclose($fp);
break;

default:
$who = str_replace("_", " ", "$who");
echo("Sign $sn's Profile Guestbook\n");
echo("<form name=\"book\">\n");
echo("<input type=hidden name=\"option2\" value=\"sign\">\n");
echo("<input type=hidden name=\"sn\" value=\"$sn\">\n");
echo("Screen Name:<input type=\"text\" name=\"who\" value=\"$who\"><br>");
echo("Message:<textarea name=\"gbook\" wrap=\"physical\" rows=\"5\" cols=\"39\"></textarea><br>");
echo("<input type=\"submit\" value=\"Sign It\"></center>");
echo("</form>");
break;
So could someone please tell me what I have wrong?

scoutt
02-04-2003, 03:42 PM
instead of a+ use r+

Bouley86
02-06-2003, 05:02 PM
well now for some reason it's not rewriting the old data. When someone tries to add something its just overwrites all the old stuff with the new stuff. Any thoughts?

Bouley86
02-06-2003, 05:14 PM
alrite..so now i do have a question i dont know the answer to. When someone signs the guestbook that i have successfully created (thanks for the help :)) it makes a gbook.txt file in the user's folder. But now say i need to delete a user..like one i just wanted to test scripts on, when i go to delete the files in my FTP client, it tells me that i dont have permission to delete them. So how do i go in and delete these files and folders. Any thoughts?

scoutt
02-06-2003, 05:21 PM
ok try this

$oldmask = umask(0);
$file_name = "$dir/$sn/gbook.txt";
$fp=fopen("$file_name", "a+");
flock($fp, 2);
$old_data = fread($fp, ($file_name));
rewind($fp);
fwrite($fp, "<a href=\"aim:goim?ScreenName=$who\">$who</a>: $gbook<br>\n" . $old_data);
flock($fp,3);
fclose($fp);
chmod($file_name, 0777);
umask($oldmask);

don't ask but it just works. :)

Bouley86
02-06-2003, 05:43 PM
um..yeah..it didnt work. So any other ideas? Like is there a script i can use that you enter the file and the path and it just deletes the file? Mad confused, any thoughts?

scoutt
02-06-2003, 05:46 PM
umm yeah did you get an error?

try this way


$oldmask = umask(0);
$file_name = "$dir/$sn/gbook.txt";
$fp=fopen("$file_name", "a+");
flock($fp, 2);
$old_data = fread($fp, ($file_name));
rewind($fp);
fwrite($fp, "<a href=\"aim:goim?ScreenName=$who\">$who</a>: $gbook<br>\n" . $old_data);
flock($fp,3);
fclose($fp);
exec(chmod $file_name 0777);
umask($oldmask);


if you want to delete a file you use unlink(file_name) and if you want to delete a folder you use rmdir($dir);

but you have to have the folder empty first.

Bouley86
02-06-2003, 05:59 PM
when i add that the gbook.php?sn=whatever page comes up blank..

Bouley86
02-06-2003, 06:26 PM
well i figured out that the problem occurs with the
exec(chmod $file_name 0777);
line but i dunno how to fix it.

Bouley86
02-06-2003, 06:30 PM
well i changed that line to
chmod ("$file_name", 0777);
but i still can't delete the file in my FTP client. Is this something im still doing wrong or should i contact the people who provide my server and ask for help?

Paul
02-06-2003, 07:13 PM
Just chmod the file to 777 in your FTP client and delete.
Good luck,
Paul

Bouley86
02-06-2003, 07:17 PM
when i try and chmodd it it says "550 gbook.txt: Operation Not Permitted"

Bouley86
02-06-2003, 07:19 PM
and when i try and delete it it says "550 gbook.txt: Permission Denied"

scoutt
02-06-2003, 09:49 PM
that is why I said to do this

$oldmask = umask(0);
$file_name = "$dir/$sn/gbook.txt";
$fp=fopen("$file_name", "a+");
flock($fp, 2);
$old_data = fread($fp, ($file_name));
rewind($fp);
fwrite($fp, "<a href=\"aim:goim?ScreenName=$who\">$who</a>: $gbook<br>\n" . $old_data);
flock($fp,3);
fclose($fp);
chmod($file_name 0777);
umask($oldmask);

and I told you to use unlink() on the file.

if you run this in the directory right above the the one you cannot delete it should delete it.

$file_name = "$dir/$sn/gbook.txt";
unlink($file_name);