filehandling - some help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

filehandling - some help

Post 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;

}

?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

What error messages, if any, does this produce?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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 :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

k good ;)
i'll test it out now
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.....
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what about this

Code: Select all

$fp = @fopen ($filename, "r") or die($error_handler("fopen1")); //Open file for reading
Mark
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

so die exits the sub?, right ;)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

die stops execution altogether, for mor info, read here [php_man]die[/php_man]

Mark
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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..

:)
Post Reply