Page 1 of 2

filehandling - some help

Posted: Tue Dec 16, 2003 11:55 am
by vigge89
I've got the following code for a administrator function on my site, but my brain have taken vacation, so I can't make the new links appear at the top of the file. This is my code:

Code: Select all

<?php

function addlink($name, $url, $desc) {

$filename = "links.txt"; //Database file (flat)

$error = "Succesful!";

if (empty($name) || empty($url) || empty($desc)) { $error = "input"; };

// Open file
$fp = @fopen ($filename, "r+") or $error = "fopen";

//Read from file
$content = @fread($fp, filesize($filename)) or $error = "fread"; 

//Write variable
$wcontent = "$name|$url|$desc\n";

@fwrite ($fp, $wcontent) or $error = "fwrite";

@fclose ($fp);

echo $error;

return $error;

}

?>

Posted: Tue Dec 16, 2003 12:12 pm
by m3mn0n
What error messages, if any, does this produce?

Posted: Tue Dec 16, 2003 12:21 pm
by vigge89
none, its just that i want to save the new links at the top of the file, which I have forgot how to do since my rain have taken vacation. IE, should I open file with "r+"? Currently, the new inputs appear at the bottom. How could this script be changed to let the new inputs appear at the top instead of bottom?

Edit: BTW, it's just an beta for the one i will be using, so the output etc. will be different

Posted: Tue Dec 16, 2003 12:53 pm
by dull1554
there is no way to place the file cursor at the top of the file, so you can write to the top, you have to copy the current contents of the txt file to a $var, and write the new stuff with w, then write the $var withr

Posted: Tue Dec 16, 2003 2:07 pm
by DuFF
If you store all of the links in an array before you echo them on the links page, then you can just use array_reverse instead of changing the where the link is written to the file.

Posted: Wed Dec 17, 2003 12:38 am
by vigge89
so u mean I open the file with file(), and then reverse the array result of it?

could you just write me example of that, please?
Don't bother if you don't have time to do that, but i'd like to see how it's done

Posted: Wed Dec 17, 2003 8:32 am
by vigge89
anyone?
just a script that opens file and adds every line into an array, and then saves the file again with the new input at the top.

Posted: Wed Dec 17, 2003 8:44 am
by Draco_03
A quick google search that took me 35 sec :P

Code: Select all

<?php
function write_beg($filename, $data)
{
//Imports old data
$handle = fopen($filename, "r");
$old_content = fread($handle, filesize ($filename));
fclose($handle);

//Sets up new data
$final_content = $data.$old_content;

//Writes new data
$handle2 = fopen($filename, "w");
$finalwrite = fwrite($handle2, $final_content);
fclose($handle2);
} 
?>
Hope it helped...if not you can slap me...
cheers :)

Posted: Wed Dec 17, 2003 8:49 am
by vigge89
k good ;)
i'll test it out now

Posted: Wed Dec 17, 2003 9:09 am
by vigge89
it works great, thx :D

BTW, i have another question;
if a function wasn't able to run, or got errors, how do you goto another part of the script, like in VB's "On Error goto errorhandler", or "goto error"?
IE, i have this in my script:

Code: Select all

<?php
$fp = @fopen ($filename, "r") or $error = "fopen1"; //Open file for reading
?>
but if the function "fopen" wasn't able to execute or got errors, it should goto another part of the script, ie, the error handling part, and skip everything else.....

Posted: Wed Dec 17, 2003 9:15 am
by JayBird
what about this

Code: Select all

$fp = @fopen ($filename, "r") or die($error_handler("fopen1")); //Open file for reading
Mark

Posted: Wed Dec 17, 2003 9:22 am
by vigge89
so die exits the sub?, right ;)

Posted: Wed Dec 17, 2003 9:24 am
by JayBird
die stops execution altogether, for mor info, read here [php_man]die[/php_man]

Mark

Posted: Wed Dec 17, 2003 10:12 am
by vigge89
so if i include that file with the script in another PHP-file, and it errors, won't the rest of the script execute? IE:
include.php:

Code: Select all

<?php
$fp = @fopen ($filename, "r") or $error = "fopen1"; //Open file for reading

?>
somepage.php:

Code: Select all

<?php
include("include.php");

echo "sometext";

?>
Won't PHP echo "sometext" if it occurs an error?

Posted: Wed Dec 17, 2003 10:15 am
by Draco_03
no it's exactly as if you writted

Code: Select all

<?php
<?php
$fp = @fopen ($filename, "r") or $error = "fopen1"; //Open file for reading

?> 

echo "sometext";

?>
Basically include only put exactly what you have in your file and past it exactly ... so usually in a iunclude file you don't have a <html> or a <body> depending on what you includ.. you understand..

:)