This is a discussion on reading an html file into a string within the PHP Language forums, part of the PHP Programming Forums category; I'm trying to read an html file from my local server into a string, I'm using the following ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm trying to read an html file from my local server into a string, I'm
using the following code: $attfile = $attachment[$i]; //create filenames $file_name = basename ($attfile); $lines = file($attfile); //get file into array foreach ($lines as $line_num => $line) { //concatenate each line $fcontent.= $line; } This should just load the file as an array then go through each line and add it to the end of the $fcontent variable. It does this but on larger files (4000+ lines of html) it stops part way through a line ver close to the end. Basically it misses off about 10-15 lines at the end of the html file I've increased the size of the file and it always seems to stop near the end, but the line where it quit before is finished. So I guess it's not some random character combination excaping it or reaching some kind of buffer limit. Does anyone have any ideas or know a simple script that will grab the contents of an html file and put it into a string (not an array). I'm using 4.2.2 with no chance of upgrading so I can't use file_get_contents. Thanks Alex |
|
|||
|
Alex Hopson wrote:
> I'm trying to read an html file from my local server into a string $html = implode(file("yourfile.html")); Hope that helps, Kelv :) -- LoHost http://www.lohost.com |
|
|||
|
["Followup-To:" header set to comp.lang.php.]
Alex Hopson wrote: > I'm trying to read an html file from my local server into a string, I'm > using the following code: > > $attfile = $attachment[$i]; //create filenames > $file_name = basename ($attfile); > $lines = file($attfile); //get file into array > > foreach ($lines as $line_num => $line) { //concatenate each line > $fcontent.= $line; > } <snip> > Does anyone have any ideas or know a simple script that will grab the > contents of an html file and put it into a string (not an array). I'm using > 4.2.2 with no chance of upgrading so I can't use file_get_contents. What happens when you do $fcontent = implode('', file($attfile)); instead? -- Mail to my "From:" address is readable by all at http://www.dodgeit.com/ == ** ## !! ------------------------------------------------ !! ## ** == TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>) may bypass my spam filter. If it does, I may reply from another address! |
|
|||
|
"Alex Hopson" <alex*under_score*hopson@hotmail.com> wrote in message
news:Of2rd.295$246.44@fe53.usenetserver.com... > I'm trying to read an html file from my local server into a string, I'm > using the following code: > > $attfile = $attachment[$i]; //create filenames > $file_name = basename ($attfile); > $lines = file($attfile); //get file into array > > foreach ($lines as $line_num => $line) { //concatenate each line > $fcontent.= $line; > } > > This should just load the file as an array then go through each line and add > it to the end of the $fcontent variable. It does this but on larger files > (4000+ lines of html) it stops part way through a line ver close to the end. > Basically it misses off about 10-15 lines at the end of the html file > > I've increased the size of the file and it always seems to stop near the > end, but the line where it quit before is finished. So I guess it's not some > random character combination excaping it or reaching some kind of buffer > limit. > > Does anyone have any ideas or know a simple script that will grab the > contents of an html file and put it into a string (not an array). I'm using > 4.2.2 with no chance of upgrading so I can't use file_get_contents. > > Thanks > Alex Don't see anything obvious. It could be a memory issue. Doing string concatention inside a loop has a way of eating up memory (because each iteration require the allocation of a bigger buffer). The simplest solution is to write your own file_get_contents(): function file_get_contents($path) { return fread(fopen($path, "rb"), 2147483647); } |
|
|||
|
On Tue, 30 Nov 2004 17:45:37 -0000, "Alex Hopson"
<alex*under_score*hopson@hotmail.com> wrote: >I'm trying to read an html file from my local server into a string, I'm >using the following code: > > $attfile = $attachment[$i]; //create filenames > $file_name = basename ($attfile); > $lines = file($attfile); //get file into array > > foreach ($lines as $line_num => $line) { //concatenate each line > $fcontent.= $line; > } > >This should just load the file as an array then go through each line and add >it to the end of the $fcontent variable. It does this but on larger files >(4000+ lines of html) it stops part way through a line ver close to the end. >Basically it misses off about 10-15 lines at the end of the html file > >I've increased the size of the file and it always seems to stop near the >end, but the line where it quit before is finished. So I guess it's not some >random character combination excaping it or reaching some kind of buffer >limit. > >Does anyone have any ideas or know a simple script that will grab the >contents of an html file and put it into a string (not an array). I'm using >4.2.2 with no chance of upgrading so I can't use file_get_contents. <?php function readinfile($template_file) { # Variable Declaration $i; $output; # Read file into temporary array $temp = file($template_file); # Construct template output for ($i=0; $i<count($temp); $i++) { $output .= $temp[$i]; } # Return results return $output; } // End readinfile ?> Then: $html = readinfile("/whatever/your/file/path/is/filename.html"); or $html = readinfile("filename.html"); and of course, you can use a variable: $source = "/whatever/your/file/path/is/filename.html"; $html = readinfile($source); -- gburnore@databasix dot com --------------------------------------------------------------------------- How you look depends on where you go. --------------------------------------------------------------------------- Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³ Black Helicopter Repair Svcs Division | Official Proof of Purchase ================================================== ========================= Want one? GET one! http://signup.databasix.com ================================================== ========================= |
|
|||
|
Do something like that :
$fp = fopen($my_file,"r"); $f_content = fread($fp,filesize($my_file)); fclose($fp); Pedro Graca wrote: > ["Followup-To:" header set to comp.lang.php.] > Alex Hopson wrote: > >>I'm trying to read an html file from my local server into a string, I'm >>using the following code: >> >> $attfile = $attachment[$i]; //create filenames >> $file_name = basename ($attfile); >> $lines = file($attfile); //get file into array >> >> foreach ($lines as $line_num => $line) { //concatenate each line >> $fcontent.= $line; >> } > > <snip> > >>Does anyone have any ideas or know a simple script that will grab the >>contents of an html file and put it into a string (not an array). I'm using >>4.2.2 with no chance of upgrading so I can't use file_get_contents. > > > What happens when you do > > $fcontent = implode('', file($attfile)); > > instead? |
|
|||
|
Doesn't anyone read the manual these days? PHP has a function for doing
just that. $fcontent=file_get_contents($file); :) "Alex Hopson" <alex*under_score*hopson@hotmail.com> wrote in message news:Of2rd.295$246.44@fe53.usenetserver.com... > I'm trying to read an html file from my local server into a string, I'm > using the following code: > > $attfile = $attachment[$i]; //create filenames > $file_name = basename ($attfile); > $lines = file($attfile); //get file into array > > foreach ($lines as $line_num => $line) { //concatenate each line > $fcontent.= $line; > } > > This should just load the file as an array then go through each line and > add it to the end of the $fcontent variable. It does this but on larger > files (4000+ lines of html) it stops part way through a line ver close to > the end. Basically it misses off about 10-15 lines at the end of the html > file > > I've increased the size of the file and it always seems to stop near the > end, but the line where it quit before is finished. So I guess it's not > some random character combination excaping it or reaching some kind of > buffer limit. > > Does anyone have any ideas or know a simple script that will grab the > contents of an html file and put it into a string (not an array). I'm > using 4.2.2 with no chance of upgrading so I can't use file_get_contents. > > Thanks > Alex > > |
|
|||
|
On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.com> wrote:
>Doesn't anyone read the manual these days? Must you top post? -- gburnore@databasix dot com --------------------------------------------------------------------------- How you look depends on where you go. --------------------------------------------------------------------------- Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³ Black Helicopter Repair Svcs Division | Official Proof of Purchase ================================================== ========================= Want one? GET one! http://signup.databasix.com ================================================== ========================= |
|
|||
|
oh you're one of those.
"Gary L. Burnore" <gburnore@databasix.com> wrote in message news:coo1o9$ole$1@blackhelicopter.databasix.com... > On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.com> wrote: > >>Doesn't anyone read the manual these days? > > Must you top post? > -- > gburnore@databasix dot com > --------------------------------------------------------------------------- > How you look depends on where you go. > --------------------------------------------------------------------------- > Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ > | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ > DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ > | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³ > Black Helicopter Repair Svcs Division | Official Proof of Purchase > ================================================== ========================= > Want one? GET one! http://signup.databasix.com > ================================================== ========================= |