Solved

Using file_put_contents() to insert dynamic content into HTML file. Is it possible?

Posted on 2013-11-18
15
790 Views
Last Modified: 2013-11-19
Experts,

I am trying to build a page dynamically using the file_put_contents() function. I am wondering if this is possible?

Example:

My file (our_employees.php) builds a dynamic page by querying several data sources and returning the results.

Is it possible to use the file_put_contents() function to create a new page which has the rendered results of the our_employees.php page as the content?

Wrong syntax but, trying to do:

file_put_contents("our_employees.html", RENDERED RESULTS OF our_employees.php);
0
Comment
Question by:evibesmusic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
15 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 39658028
file_put_contents("our_employees.html", file_get_contents("http://www.site.com/myfile.php"));

or for readability

$getfile=file_get_contents("http://www.site.com/myfile.php");
file_put_contents("our_employees.html", $getfile);
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 39658030
I wouldn't do it that way.  I would use the 'include' function in PHP to 'include' the new content in the page.  http://us3.php.net/manual/en/function.include.php
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658032
Dave is right, unless there is some reason not to.
Hmmm, are you just trying to create a static html version of the files.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39658047
After re-reading your question, it appears that you are just making a second copy of 'our_employees.php'.  Why?  Why not just use 'our_employees.php' each time so it will be current?
0
 

Author Comment

by:evibesmusic
ID: 39658059
@All:

Sorry for the difficulty in articulating what I need.

I think I've answered my own question using the internal buffering provided by the ob_start(); and ob_get_contents(); functions.

I basically wanted to capture the rendered HTML produced by our_employees.php, and copy the HTML into a file called our_employees.html. The following seems to be producing and saving what I need.

Can either of you think of why I wouldn't want to do it this way? Pitfalls?

I intend to ultimately have this process run once per day on our local server in order to keep the content of our_employees.html up to date.

<?php
//START INTERNAL CACHING
ob_start();
//INCLUDE THE PAGE WE WANT RENDERED
include(our_doctors.php);
//GET THE CONTENTS OF THE INTERNAL CACHE AND STORE IN VARIABLE
$content = ob_get_contents();
//CREATE AND/OR RE-WRITE THE DESIRED PAGE
//ADD CACHED CONTENT TO BODY OF PAGE
file_put_contents("our_doctors.html",$content);
?>
0
 

Author Comment

by:evibesmusic
ID: 39658063
@DaveBaldwin:

Re: "After re-reading your question, it appears that you are just making a second copy of 'our_employees.php'.  Why?  Why not just use 'our_employees.php' each time so it will be current? "

Good question.

I am attempting to come up with a creative solution here.

The server which houses the our_employees.html file is unable to connect to several databases. Otherwise, I would as you suggest just use the PHP version of the file which would then build the page (and its contents on the fly).

The thought was to have the script I shared above, run once per day. Therefore, the our_employee.html file will be updated at least daily if I can't have a constant connection to the data sources needed.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658066
Still a bit confused - so our_employees.html is on a different server from our_employees.php.
0
 

Author Comment

by:evibesmusic
ID: 39658071
@GaryC123:

Correct.

After getting the cached HTML from our_employees.php, and creating the our_employees.html file, I will use PHP to upload our_employees.html to the other server.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658076
You could always use the same example I listed above to get it from the other server.
file_get_contents (or curl) can be used for remote files.
I don't see includes being of any use here in this case.
0
 

Author Comment

by:evibesmusic
ID: 39658078
@All:

Thanks for the help. I will try using the alternate methods both of you have suggested.

Happy Holidays from California!

:)
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39658109
If PHP and allow_url_fopen are enabled on the second server, you can 'include' the HTML from 'our_employees.php' on the first server in a file on the second server by putting the 'http://' in the file name.

include 'http://firstserver/our_employees.php'

or use the IP address.

include 'http://10.10.10.10/our_employees.php'

That will cause the 'our_employees.php' to run on the first server and return the results in the page on the second server.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39658124
allow_url_fopen = dangerous.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39658628
Why do you say that it's dangerous?  I thought about it for a while and I can't see how that is true.  Half of the web hosts I use have it On and half have it Off.
0
 

Author Comment

by:evibesmusic
ID: 39659952
@DaveBaldwin:

One of the servers is public, the other is not. I won't be able to use your suggestion.

I am however interested in your conversation with @GaryC123 regarding the use of the allow_url_fopen() command.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39660100
In itself it is not dangerous, but could be utilised for cross site scripting if your site/server was hacked.
You would find most shared hosting will not allow it.
In your situation there is no reason to use it because you have other avenues that can do the same thing
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question