Solved

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

Posted on 2013-11-18
15
743 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

809 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