Solved

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

Posted on 2013-11-18
15
728 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parts and Products table schema in mysql 6 43
Form Processing in PHP 11 31
only allow numbers with preg match 4 25
hiding/removing php extension - best practice 4 24
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now