Results 1 to 7 of 7
Thread: PHP Help
-
05-06-2011, 02:24 PM #1Newbie
- Join Date
- May 2011
- Location
- Manchester, UK
- Posts
- 12
PHP Help
Hey,
I'm not sure what section this should go in but here it goes anyway.
I need to basically grab some data from an external .txt file. I only need the bottom line of the file. I know a code which will echo the top value of the file, anyone know how to change this to the bottom value?
Here's the code:
Code:$myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); $theData = fgets($fh); fclose($fh); echo $theData;
-
05-06-2011, 03:29 PM #2Web Hosting Guru
- Join Date
- Apr 2008
- Posts
- 334
Hi there,
Here's one (efficient) way to achieve what you want :
PHP Code:$handle = fopen('file.txt', 'r');
$pos = -1;
// Seeks file's end.
fseek($handle, $pos, SEEK_END);
$string = '';
$char = fgetc($handle);
// Read every characters of the file, one by one, from the end,
// until an end of line delimiter is found or until we've reached
// begining of the file.
while (($char !== false) && ($char != "\r") && ($char != "\n"))
{
fseek($handle, $pos--, SEEK_END);
$char = fgetc($handle);
$string = $char . $string;
}
fclose($handle);
echo $string;
-
05-06-2011, 05:08 PM #3Newbie
- Join Date
- May 2011
- Location
- Manchester, UK
- Posts
- 12
Hey WootWoot,
I really appreciate your reply. I have tried out this code, looks like it should work and I got some kind of PHP error. It doesn't make much sense to me can you make anything out of it?
Warning: fseek() [function.fseek]: stream does not support seeking in /home/directory/index.php on line 7
Warning: fseek() [function.fseek]: stream does not support seeking in /home/directory/index.php on line 17
-
05-06-2011, 05:28 PM #4Web Hosting Guru
- Join Date
- Apr 2008
- Posts
- 334
Hum... This piece of code shall work fine.
Have you replaced "file.txt" with your own file name? Is it a .txt file? Are you sure this file exists and is properly CHMODed?
If the file isn't that big, you may simply use :
PHP Code:$lines = file('file.txt');
echo $lines[count($lines) - 1];
-
05-08-2011, 04:57 AM #5WHT Addict
- Join Date
- Apr 2011
- Location
- Charlotte, NC
- Posts
- 104
In the case of large files, I typically use this method:
PHP Code:$file = 'myfile.txt';
$lines = 1;
$file = escapeshellarg($file);
$lines = escapeshellarg($lines);
exec("tail -n $lines $file\n",$output);
-
05-08-2011, 06:11 AM #6Newbie
- Join Date
- May 2011
- Location
- Manchester, UK
- Posts
- 12
Hello,
Thanks both of those worked. I appreciate your time in helping me out with this.
CowHosts
-
05-08-2011, 09:19 PM #7Newbie
- Join Date
- Apr 2011
- Posts
- 18
Thanks for the reply good solution
Similar Threads
-
nginx + php-fpm + debian squeeze tutorial - the fastest way to host php!
By AltruHost in forum VPS TutorialsReplies: 33Last Post: 07-25-2011, 01:01 PM -
Windows- PHP exploit being used to render all PHP sites useless, need urgent help,!
By webuser99 in forum Hosting Security and TechnologyReplies: 11Last Post: 03-24-2008, 05:44 AM -
PHP: Custom WHMCS billing gateway or PHP script - API included
By omega36 in forum Employment / Job OffersReplies: 1Last Post: 11-29-2007, 03:01 PM -
php safe_mode on and /usr/lib/php/DB.php error - pear
By nand in forum Hosting Security and TechnologyReplies: 1Last Post: 05-03-2005, 02:44 AM -
Want to offer your customers your own PHP Encoder,Editor,PHP Project Wizard ($25)
By geosters in forum Other Offers & RequestsReplies: 1Last Post: 02-15-2005, 07:44 PM