Results 1 to 7 of 7

Thread: PHP script to download html source code and save it to file.

  1. #1
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP script to download html source code and save it to file.

    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?

  2. #2
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    <?php
    $handle = file_get_contents($_GET[where]);
    $f=@fopen("dump.txt","a");
    fwrite($f,$handle);
    fclose($f);
    ?>

    URL Format:

    .php?where=http://www.url.to/page/

    Go Crazy. Adam West Crazy.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.

    <?
    ob_start();
    include('$_GET['where']);
    $handle = ob_get_contents();
    ob_end_flush();
    ...
    ?>

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I don't believe that file_get_contents() is designed for remote files
    URL file-access has to be enabled on the server.
    - Mike

  5. #5
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Tested it to get the Contents Of A Few Different Pages.

    it Worked, so i didn't think there was a problem.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Jan 2009
    Location
    Brooklyn, NY
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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:
    PHP Code:
    <?php
    $handle 
    file_get_contents($_GET[contract.php]);
    $f = @fopen("contract.doc","+w");
    fwrite($f,$handle);
    fclose($f);
    ?>
    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....

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •