I have created a PHP file which is set to read the contents of a local text file, parse into an array, and then populate a table within the main page. However, even just 53 lines of this text file takes around 10 seconds to load the page before any content is displayed.
I understand that this is a limitation where the server needs to create the entire html file before sending it to clients, and this generation takes time depending on the size of the content to generate from, meaning this 10 seconds will likely increase with more lines to process. The end product will have well over 500 lines to process, so this will take minutes to generate, and then send to the client.
I was wondering if I could bypass this, send the client a small html file holding an empty table, and then start to fill and populate this table using PHP, but I am unsure of how to go about this. Would I call an include to a separate php file which will call the generation code, or get the server to 'stream' the content one table row at a time?
The page in question is: http://zionfox.net/cards.php. The current load time takes around 7-10 seconds, and I'd like to decrease this by only loading the core table, then displaying the table rows as they are generated by the source.
Edit: There is a reason for the larger images too: When zooming in on mobile devices, I'd rather the images be as crisp and sharp as the fonts alongside. I tried with actual size images (20px) and they became almost unreadable when zoomed in. I realise that .png is probably not the best format to use, but it's my first time on using file_put_contents()
and I'm not entirely sure of the source file's image, nor making sure the images retain transparency.
Edit: Edit: The source to the file is: http://zionfox.net/cards.php.txt - Please excuse the scruffiness of the code, it's still heavily work in progress and I wanted to get it working first before tidying the code.
The file is split into an array separated by characters using explode()
. Each of these arrays are then split into further arrays using more explode()
s and then each part of this array is processed to see if criteria match, and return the correct results based on the values, including, pulling images from external sources if I don't already have the image. This is all then echo
ed out at the bottom of the for loop to create a new table row, then it repeats the process for the next line.
Thanks to Tenhsor, the solution to this:
$handle = fopen($pathToFile, "r");
if(ob_get_level() == 0) ob_start();
if($handle) {
while(($line = fgets($handle)) !== false) {
echo $row . "<br />";
//The code you want to process
ob_flush();
flush();
}
}
ob_end_flush();
fclose($handle);
Full solution found in their response.
I also want to add, this code alone didn't solve my issue. I was running IIS 7.5 and had to issue a command in order to make it work properly. The command depends on your PHP version, which you should be able to find through IIS. The command to give to cmd.exe is:
%windir%\system32\inetsrv\appcmd.exe set config /section:handlers "/[name='PHP53_via_FastCGI':ResponseBufferLimit:0"
Where PHP53_via_FastCGI
is the name and version of your PHP.
You can use the flush php function to send chunks of information to the client. for example, if you are showing a html table using a loop you can do something like this:
file = @fopen('path/to/my/file', 'rb');
$buffer = 1024 * 8;
echo '<table>';
while (!feof($file)) {
echo '<tr><td>'.fread($file, $buffer).'</td></tr>';
ob_flush();
flush();
}
echo '</table>';
ob_end_flush();