• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

PHP Fopen Speed

Dear Experts,

I am building a crm system for a client, and I am just doing some basic template management & design.

When including these templates I currently have a function that fopens the file (with read header) then fread's the contents, and replaces what it needs to replace, then outputs that to the requester.

Now this is fine, however some elements inside those pages require other template files to be included for example the search box appears everywhere so I use a template for it. Now my script currently seperately fopens the file every time that search box needs to be shown.

Is that an OK way of doing it, or am I just wasting I/O's and, should I just use a database and put all the much repeated sections in there?

Or should I, in fact use a file containing certain functions that would just display the output from a variable inside that very file?

Cheers for your responces.

  • 2
1 Solution
If you're replacing with the same the same values over and over again, you'd be better off caching the final result. Make a temporary directory called "cache" (or something like that) and before you do a replacement, check to see if there's a cached version available. If not, create one after you do your replacements. If there are different ways of doing the replacements, then just name your cached files in such a way that you can identify which version each is.
You really should be using output buffers for this, you'll find it much easier to work with and allows you to 1) use a regular include and 2) use variables within the script your including.
include "file.php";
$contents = ob_get_clean();

Open in new window

A note to animecyc's suggestion - if you do include it, just be aware that you're actually RUNNING that PHP file and capturing the output. So if your PHP script does anything that might make changes to the database or has any sort of significant loading time, then using fopen would be better. Otherwise, if you don't mind the PHP executing, then output buffering would be a good way (caching would still be a good method, no matter which way you go)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now