473,749 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Read Text File

Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file. Problem is this...whenever
the file gets larger that ~5MB, the page just displays nothing, as
though a timeout has occurred but I get no error. At 4.8MB (last
confirmed size)...the function still works. Any ideas what code below
is lacking??

<?
$handle= fopen("/var/log/myfile", "r");
if ($handle) {
while (!feof($handle) ) {
$arrLog[] = fgets($handle, 4096);
}
fclose($handle) ;
}

$int_number_of_ lines = count($arrLog);
if ($int_number_of _lines == 0)
{
echo '<p><strong>No lines read.</strong></p>';
}
if ($int_number_of _lines < $int_lines)
{
$int_lines = $int_number_of_ lines;
}
$int_firstline = $int_number_of_ lines - $int_lines;
echo 'Showing the last '.$int_lines.' lines out of '.
$int_number_of_ lines.'<BR />';
echo "<TABLE WIDTH=100% CLASS=\"mail\"> \n";
for ($i=$int_firstl ine; $i<$int_number_ of_lines; $i++)
{
echo "<TR><TD>".$arr Log[$i]."</TR></TD>\n";
}
echo "</TABLE>\n";

?>
Jun 2 '08 #1
28 3274
tl****@gmail.co m wrote:
Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file.
<?php
passthru("tail -n $lines $file");
?>

Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Proudly running Debian Linux with 2.6.24-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.5-3 generating this signature.
Uptime: 00:28:14 up 10 days, 15:27, 2 users, load average: 0.89, 0.35,
0.18

Jun 2 '08 #2
<comp.lang.ph p>
<>
<Sat, 19 Apr 2008 14:09:39 -0700 (PDT)>
<ce************ *************** *******@a70g200 0hsh.googlegrou ps.com>
Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file. Problem is this...whenever
the file gets larger that ~5MB, the page just displays nothing, as
though a timeout has occurred but I get no error. At 4.8MB (last
confirmed size)...the function still works. Any ideas what code below
is lacking??
$build="/var/log/myfile";
$contents=file_ get_contents($b uild);
$demo=explode(" \n",$contents );

Something like the above might be worth a try .
For the benefit of any newbies .....

The above reads in the file in the one go and $demo in effect becomes
$demo[0] $demo[1] $demo[2] etc etc via the explode command - the
advantage being the above code is much faster than reading in the text
file one line at a time .
--
www.krustov.co.uk
Jun 2 '08 #3
On Apr 19, 5:09 pm, tlp...@gmail.co m wrote:
Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file. Problem is this...whenever
the file gets larger that ~5MB, the page just displays nothing, as
though a timeout has occurred but I get no error. At 4.8MB (last
confirmed size)...the function still works. Any ideas what code below
is lacking??

<?
$handle= fopen("/var/log/myfile", "r");
if ($handle) {
while (!feof($handle) ) {
$arrLog[] = fgets($handle, 4096);
}
fclose($handle) ;

}

$int_number_of_ lines = count($arrLog);
if ($int_number_of _lines == 0)
{
echo '<p><strong>No lines read.</strong></p>';}

if ($int_number_of _lines < $int_lines)
{
$int_lines = $int_number_of_ lines;}

$int_firstline = $int_number_of_ lines - $int_lines;
echo 'Showing the last '.$int_lines.' lines out of '.
$int_number_of_ lines.'<BR />';

echo "<TABLE WIDTH=100% CLASS=\"mail\"> \n";
for ($i=$int_firstl ine; $i<$int_number_ of_lines; $i++)
{
echo "<TR><TD>".$arr Log[$i]."</TR></TD>\n";
}
echo "</TABLE>\n";

?>
The problem is probably due to the php.ini configuration of
max_execution_t ime. I forget the default but it's about 30 seconds.
Try jacking the value up and see if it keeps executing. Though any
script which takes more than 30 seconds is probably not the best
solution either. Consider dumping the lines into a mysql table and
searching that way.
Jun 2 '08 #4
tl****@gmail.co m wrote:
Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file. Problem is this...whenever
the file gets larger that ~5MB, the page just displays nothing, as
though a timeout has occurred but I get no error. At 4.8MB (last
confirmed size)...the function still works. Any ideas what code below
is lacking??

<?
$handle= fopen("/var/log/myfile", "r");
if ($handle) {
while (!feof($handle) ) {
$arrLog[] = fgets($handle, 4096);
}
fclose($handle) ;
}

$int_number_of_ lines = count($arrLog);
if ($int_number_of _lines == 0)
{
echo '<p><strong>No lines read.</strong></p>';
}
if ($int_number_of _lines < $int_lines)
{
$int_lines = $int_number_of_ lines;
}
$int_firstline = $int_number_of_ lines - $int_lines;
echo 'Showing the last '.$int_lines.' lines out of '.
$int_number_of_ lines.'<BR />';
echo "<TABLE WIDTH=100% CLASS=\"mail\"> \n";
for ($i=$int_firstl ine; $i<$int_number_ of_lines; $i++)
{
echo "<TR><TD>".$arr Log[$i]."</TR></TD>\n";
}
echo "</TABLE>\n";

?>
Probably running out of memory in PHP...

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #5
On Apr 19, 5:28 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
tlp...@gmail.co m wrote:
Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file.

<?php
passthru("tail -n $lines $file");
?>

Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Proudly running Debian Linux with 2.6.24-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.5-3 generating this signature.
Uptime: 00:28:14 up 10 days, 15:27, 2 users, load average: 0.89, 0.35,
0.18
First, thank you for offering suggestions. I'll research using tail
but my initial problem with it is that it doesn't create an arrary
where I can display each line on a table row but I may be doing
something wrong...

$myfile = "/var/log/maillog";
$arrLog[]=passthru("tail -n $int_lines $myfile");

$int_number_of_ lines = count($myfile);
if ($int_number_of _lines == 0)
{
echo '<p><strong>No lines read.</strong></p>';
}
if ($int_number_of _lines < $int_lines)
{
$int_lines = $int_number_of_ lines;
}
$int_firstline = $int_number_of_ lines - $int_lines;
echo 'Showing the last '.$int_lines.' lines out of '.
$int_number_of_ lines.'<BR />';
echo "<TABLE WIDTH=100% CLASS=\"mail\"> \n";
for ($i=$int_firstl ine; $i<$int_number_ of_lines; $i++)
{
echo "<TR><TD>".$arr Log[$i]."</TR></TD>\n";
}
echo "</TABLE>\n";
Jun 2 '08 #6
On Apr 19, 6:56 pm, venti <timgreg...@shi eldinvestmentgr oup.com>
wrote:
On Apr 19, 5:09 pm, tlp...@gmail.co m wrote:
Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file. Problem is this...whenever
the file gets larger that ~5MB, the page just displays nothing, as
though a timeout has occurred but I get no error. At 4.8MB (last
confirmed size)...the function still works. Any ideas what code below
is lacking??
<?
$handle= fopen("/var/log/myfile", "r");
if ($handle) {
while (!feof($handle) ) {
$arrLog[] = fgets($handle, 4096);
}
fclose($handle) ;
}
$int_number_of_ lines = count($arrLog);
if ($int_number_of _lines == 0)
{
echo '<p><strong>No lines read.</strong></p>';}
if ($int_number_of _lines < $int_lines)
{
$int_lines = $int_number_of_ lines;}
$int_firstline = $int_number_of_ lines - $int_lines;
echo 'Showing the last '.$int_lines.' lines out of '.
$int_number_of_ lines.'<BR />';
echo "<TABLE WIDTH=100% CLASS=\"mail\"> \n";
for ($i=$int_firstl ine; $i<$int_number_ of_lines; $i++)
{
echo "<TR><TD>".$arr Log[$i]."</TR></TD>\n";
}
echo "</TABLE>\n";
?>

The problem is probably due to the php.ini configuration of
max_execution_t ime. I forget the default but it's about 30 seconds.
Try jacking the value up and see if it keeps executing. Though any
script which takes more than 30 seconds is probably not the best
solution either. Consider dumping the lines into a mysql table and
searching that way.
Thanks, but the page returns blank instantly...no time out.
Jun 2 '08 #7
On Apr 19, 6:56 pm, venti <timgreg...@shi eldinvestmentgr oup.com>
wrote:
On Apr 19, 5:09 pm, tlp...@gmail.co m wrote:
Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file. Problem is this...whenever
the file gets larger that ~5MB, the page just displays nothing, as
though a timeout has occurred but I get no error. At 4.8MB (last
confirmed size)...the function still works. Any ideas what code below
is lacking??
<?
$handle= fopen("/var/log/myfile", "r");
if ($handle) {
while (!feof($handle) ) {
$arrLog[] = fgets($handle, 4096);
}
fclose($handle) ;
}
$int_number_of_ lines = count($arrLog);
if ($int_number_of _lines == 0)
{
echo '<p><strong>No lines read.</strong></p>';}
if ($int_number_of _lines < $int_lines)
{
$int_lines = $int_number_of_ lines;}
$int_firstline = $int_number_of_ lines - $int_lines;
echo 'Showing the last '.$int_lines.' lines out of '.
$int_number_of_ lines.'<BR />';
echo "<TABLE WIDTH=100% CLASS=\"mail\"> \n";
for ($i=$int_firstl ine; $i<$int_number_ of_lines; $i++)
{
echo "<TR><TD>".$arr Log[$i]."</TR></TD>\n";
}
echo "</TABLE>\n";
?>

The problem is probably due to the php.ini configuration of
max_execution_t ime. I forget the default but it's about 30 seconds.
Try jacking the value up and see if it keeps executing. Though any
script which takes more than 30 seconds is probably not the best
solution either. Consider dumping the lines into a mysql table and
searching that way.
Thanks, but the page returns blank instantly...no time out.
Jun 2 '08 #8
tl****@gmail.co m wrote:
On Apr 19, 6:56 pm, venti <timgreg...@shi eldinvestmentgr oup.com>
wrote:
>On Apr 19, 5:09 pm, tlp...@gmail.co m wrote:
>>Hey, read some tips/pointers on PHP.net but can't seem to solve this
problem. I have a php page that reads the contents of a file and then
displays the last XX lines of the file. Problem is this...whenever
the file gets larger that ~5MB, the page just displays nothing, as
though a timeout has occurred but I get no error. At 4.8MB (last
confirmed size)...the function still works. Any ideas what code below
is lacking??
<?
$handle= fopen("/var/log/myfile", "r");
if ($handle) {
while (!feof($handle) ) {
$arrLog[] = fgets($handle, 4096);
}
fclose($handle) ;
}
$int_number_o f_lines = count($arrLog);
if ($int_number_of _lines == 0)
{
echo '<p><strong>No lines read.</strong></p>';}
if ($int_number_of _lines < $int_lines)
{
$int_lines = $int_number_of_ lines;}
$int_firstlin e = $int_number_of_ lines - $int_lines;
echo 'Showing the last '.$int_lines.' lines out of '.
$int_number_o f_lines.'<BR />';
echo "<TABLE WIDTH=100% CLASS=\"mail\"> \n";
for ($i=$int_firstl ine; $i<$int_number_ of_lines; $i++)
{
echo "<TR><TD>".$arr Log[$i]."</TR></TD>\n";
}
echo "</TABLE>\n";
?>
The problem is probably due to the php.ini configuration of
max_execution_ time. I forget the default but it's about 30 seconds.
Try jacking the value up and see if it keeps executing. Though any
script which takes more than 30 seconds is probably not the best
solution either. Consider dumping the lines into a mysql table and
searching that way.

Thanks, but the page returns blank instantly...no time out.
Enable errors and display them. You'll see your problem.

In your php.ini file, put:

error_reporting = E_ALL
display_errors = on

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #9
Iván Sánchez Ortega wrote:
tl****@gmail.co m wrote:
>Solved! I upped the max memory in php.ini from 8M to 16M.

That doesn't solve it - it just postpones it.

Open the file, fseek() to the end, read 4Kb chunks, then strrchr() to find
the line breaks. No wasted memory.
Maybe, maybe not. It depends on how large the file gets.

Reading 4K at a time and trying to tack things together across chunks is
also requires a much larger amount of CPU.

I often give PHP 32-128MB, depending on the system and what's required
for the site. Memory is cheaper than CPU cycles.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2667
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to replace the text its reversing eg.
1
4308
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
35
11484
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
3
18961
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
3
2960
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT datatype for the file instead of using IMAGE datatype. I can not use SqlDataReader to read the data. I need your help, Thanks. -David (1) I have a table TestFile for testing: ID int FileName navrchar(255)
3
1818
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a new seek item in the textbox after I searched for an item and click the seek button.. it seems it is doing nothing. And also... what code can I use to genereate a messagebox message when a seek item is not at all in the CSV file?
6
32918
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line). Is there some way to burst read the whole file and later "extract" each line? Thanks in advance, Thomas Kowalski
6
2488
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed from user to calculate the score. Here is a problem. I can read a text file. However, it's read whole file at a time. So,
0
3870
by: alivip | last post by:
Is python provide search in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name camry and file name corola, file name honda contain folder accord and BMW contain file name X5 Is there way to enter name of parent folder(cars) and search in all sub folder(Toyota,Honda and BMW) and files ? how can I intgreat cod to be user interface (buttun ,text box...
4
3417
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a database. What I need help with is on how to locate a specific line in the file, change it and then save the updated text file. Can anyone help me out or point me to a site that explains this clearly? Here's part of my code that reads the contents of...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.