True enough, fread reads the entire file contents into RAM, and that could be a 
problem. On the other hand, if it is not a problem--if the file to be read is 
not too big--it's more efficient and there is less code to maintain. The 
regular expression in my example extracts the appropriate part of the file, 
there is no need to check individual lines of content.

Your code is returning the entire file, excluding whatever is contained in the 
<HEAD>...</HEAD>. It seems likely that if the entire file uses too much memory, 
the part your code returns is probably going to use too much memory as well.

The other problem with your code here is that is does not take into account any 
properties of the body tag that may exist, such as bgcolor, link, etc. Leaving 
the closing bracket off the <BODY> tag would probably take care of this.

There is always more than one way to accomplish any task. You'll just have to 
pick one that suits your needs.


Randy Clamons
Systems Programming
Astro-auction.com


> ------------Original Message------------
> From: The Disguised Jedi <[EMAIL PROTECTED]>
> To: php-windows@lists.php.net
> Date: Sun, Jan-16-2005 2:28 PM
> Subject: Re: [PHP-WIN] including an HTML file
>
> Well, fread reads the entire file, and we need to check the lines to
> see if they contain our tag, and so that is why i used fgets.  fread
> also puts the entire file into RAM, and that can cause memory problems
> with large files.  But, I forgot a few pieces of my function, so I'm
> adding a better version.
> 
> <?php
> 
>       $filename = 'ourfile.html';
>       $tag = '<BODY>';
>       $closing_tag = '</BODY>';
> 
> function insert_html($filename, $tag, $closing_tag) { 
>       //Open the HTML file
>       $handle = fopen($filename, 'r');
>       
>       $i = 0;
>       //As long as the file reader isn't at the end of the file, keep 
> looping through
>       while (!feof($handle)) {
>               //Read through our file in 4MB chunks, or by line, whichever 
> comes 
> first...
>               $buffer = fgets($handle, 4096);
>               //Search our buffer for the tag.  If it is there, we tell the 
> rest
> of the program to start saving out the text...
>               if (strstr($buffer, $tag) == $tag) {
>                       $save = true;
>               }
>               if ($save == true) {
>                       $file[$i] = $buffer;
>                       $i++;
>               }
>               if (strstr($buffer, $closing_tag) == $closing_tag) {
>                       $save = false;
>               }
>       }
>       //Put the data all together, and send it back...
>       while (current($file)) {
>               $data .= '
>               ' . current($file);
>       }
>       return $data;
> }
> 
> ?>
> 
> I forgot to add the part that tells it to check for when to stop
> saving.  This should suit your needs Gaetono, let me know if it
> doesn't work right!
> 
> 
> -- 
> The Disguised Jedi
> [EMAIL PROTECTED]
> 
> PHP rocks!
> "Knowledge is Power.  Power Corrupts.  Go to school, become evil"
> 
> Disclaimer: Any disclaimer attached to this message may be ignored.
> This message is Certified Virus Free
> 
> -- 
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to