Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-18
15
Medium Priority
?
881 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
  • 6
  • 5
  • 4
15 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1000 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 84

Accepted Solution

by:
Dave Baldwin earned 1000 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 84

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 84

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 84

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

886 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