View Full Version : PHP script to download html source code and save it to file.

02-01-2007, 10:05 PM
Is there a script to download an html page [such as http://google.com] and save the source code as a txt file onto the server?

02-02-2007, 12:17 AM
$handle = file_get_contents($_GET[where]);

URL Format:


Go Crazy. Adam West Crazy.

02-02-2007, 09:09 AM
But do be careful about copyrights.

Also, that isn't always going to work. Or ever, maybe. I don't believe that file_get_contents() is designed for remote files. It certainly isn't enabled as such on all servers.

$handle = ob_get_contents();

Hence, a weird workaround with output buffers is possible.

Don't abuse the code... :)
Would be easy to. Heh.

Also, there was another recent thread about this and a long discussion about the few servers that have remote files blocked for security reasons (safety mode feature, I think). Look for that (about "get title of other page" or something) if you have trouble using this... there is a more complex http-request method in that, I believe.

02-02-2007, 10:07 AM
I don't believe that file_get_contents() is designed for remote files
URL file-access has to be enabled on the server.

02-02-2007, 02:40 PM
Tested it to get the Contents Of A Few Different Pages.

it Worked, so i didn't think there was a problem.

02-02-2007, 03:04 PM
Ah, it does. I wasn't aware that it was quite that easy. The code I posted will work too. Consider it a workaround if yours doesn't. Yours is certainly fine but some servers won't support it. I *think* that the code I posted will work on more, but perhaps the same limit would apply to both. I wasn't aware of this, so not sure on the specifics.

10-16-2009, 02:06 AM
Sorry to try and revive (hopefully successfully) an old post but.....

I have tried this script and scripts like this using the fopen/fwrite classes to create ms word files....and it works BUT....the file output for this script comes up blank (0k, no content)....Let me explain how I am trying to use it....I am placing the code with-in the page I am trying to get the output of....It's a php page that generates a contract with data populated from a mysql database...So when the page is viewed, it automatically creates the file...Well, I got it to create the file but it comes up blank....Any idea why?

Here is the code:

$handle = file_get_contents($_GET[contract.php]);
$f = @fopen("contract.doc","+w");

So I replaced the 'where' with contract.php which is the page that is populated with dynamic content.

I placed this code at the very bottom of my file....What is going on? Am I missing something?

Also...I have a question regarding the file names. Instead of 'contract.doc' as the file name is it possible to set this as a dynamic file name or auto-incrementing? Ideally I would like the files to show up as 1_contract.doc, 2_contract.doc, etc...where each number shows up is relative to the "id" column in the database...so depending on the id associated with all the information populated in the page it would give it the appropriate name....