Reading file from the end to the begin

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

Post Reply
Homesick
Forum Newbie
Posts: 16
Joined: Tue Apr 06, 2010 1:53 am

Reading file from the end to the begin

Post by Homesick »

Dear All ,,
I would like to ask you a question related to reading files using php. How could I read a file from the end to the begin??
I tried to use file() function and doing reverse to the output array, this way does not make sense when the file size is too large,,

Is there any Ideas ???


Thanks alot
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Reading file from the end to the begin

Post by Jade »

A good place to start is the file functions: http://php.net/manual/en/function.fopen.php
Homesick
Forum Newbie
Posts: 16
Joined: Tue Apr 06, 2010 1:53 am

Re: Reading file from the end to the begin

Post by Homesick »

Jade wrote:A good place to start is the file functions: http://php.net/manual/en/function.fopen.php
Thanks Jade for reply,,

I did read this functions and related functions too,, I did not found such function that meet my objective :(
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Reading file from the end to the begin

Post by Jade »

I'm not sure how that couldn't meet your objective... you start by opening the file then getting all the contents, then closing the file.

Code: Select all

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Reading file from the end to the begin

Post by liljester »

im not sure of a way to actually read the file from the end to the beginning... off hand im going to say you will have to read the whole file then reverse it. what are you trying to accomplish by reading it backwards?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Reading file from the end to the begin

Post by Jade »

If you want to read it backwards you'll need to use a pointer: http://www.php.net/manual/en/function.fseek.php
Homesick
Forum Newbie
Posts: 16
Joined: Tue Apr 06, 2010 1:53 am

Re: Reading file from the end to the begin

Post by Homesick »

Thx Jade,, and sorry for being late to reply,,

I Just want to read file from the end since it contains the the last updated information that I need ,, ((business need))

any ways, I have heard about the pointer ,, I will read ur link ,,

You are really nice friend :)

thanks,,
Post Reply