Results 1 to 21 of 21
  1. #1
    Join Date
    Nov 2004
    Location
    Dallas
    Posts
    739

    PHP Import HTML external file

    Hi, I have a problem that I can't solve, I've searched in google, in php documentation and nothing...

    I have a PHP page, and I need to import plain HTML code into the page that contains the PHP code.

    The file that contains the HTML is located in the same folder...

    Please someone know how to do this?


  2. #2
    Join Date
    Sep 2003
    Location
    Pawtucket, RI USA
    Posts
    23
    Use somthing like...

    Code:
    $fd = fopen("file.html", r);
    $content = fread($fd, filesize("file.html"));
    fclose($fd);
    
    echo $content;
    Wherever you echo the $content your HTML file will be displayed. If you need further assistance feel free to PM me.
    Jeffrey J. Dodge
    RWJD.Com | [url=http://www.filehost.nu]FileHost.nu

  3. #3
    Join Date
    Nov 2004
    Location
    Dallas
    Posts
    739
    Thanks man, it works perfectly, Thanks!

  4. #4
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    660
    Even simpler:

    Code:
    echo file_get_contents( "file.html" );
    circlical - hosting software development
    forums * blog

  5. #5
    Join Date
    Oct 2002
    Location
    Middle Dearth
    Posts
    21,306
    Why not this?
    Code:
    include('file.html');
    Having problems, or maybe questions about WHT? Head over to the help desk!

  6. #6
    Quote Originally Posted by bear
    Why not this?
    Code:
    include('file.html');
    or
    require('file.html');
    if the file is mandatory and you want the loading to die if it's not found...

  7. #7
    Join Date
    Nov 2004
    Location
    Dallas
    Posts
    739
    Quote Originally Posted by El-Vino
    or
    require('file.html');
    if the file is mandatory and you want the loading to die if it's not found...
    Thanks to all of you guy's, Think I need to read again PHP for dummies...

    Thanks

  8. #8
    Join Date
    Feb 2006
    Location
    Lancashire, UK
    Posts
    473
    i use require "file.php";

  9. #9
    This is like what I have ben looking to do, only is there a way to do this if there are meany html files, and diffrent names where you dont know the names?
    odd ??? I know but if anyone could help

  10. #10
    You can always use your include in a loop, with a variable.

    Do something like open the directory and loop through all of the files. For each file, if they match a given string template, include that file.

    Should be pretty simple.

  11. #11
    no need for a loop...

    PHP Code:
    <?
    if ((isset($_GET['p'])) && (isfile($_GET['p'].'.html'))) include($_GET['p'].'.html');
    else include(
    'default.html');
    ?>
    you would call pages with index.php?p=mypage

  12. #12
    I am a little new to php, could you explain how your code would work nnormal?
    just looking at it, it looks like this still needs a file that you know the name of the file. also would this alow me to do this 10+ times in one page, and could I put this php script in a html page?

    sorry for all the ????, like I said I am a little new to php. thanks

  13. #13
    Absolutely. I just thought that atomicshockwave was talking about including multiple HTML files with unknown names into one PHP script.

  14. #14
    it wouldnt put multiple files in for that you may need a loop. are you trying to include each file from a directory or something?

    PHP Code:
    <?php
    if ($handle opendir('.')) {
       while (
    false !== ($file readdir($handle))) {
           if (
    $file != "." && $file != "..") {
              include(
    $file);
           }
       }
       
    closedir($handle);
    }
    ?>

  15. #15
    I need to have dhtml popup window for each html file in a folder on my site, and I need the script to add the window script, because files are added and removed evry day, now my plain is that in the html files would be the window script, and the php code would find the files and run the code in the index.html file (my homepage where I need the windows.

  16. #16
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    660
    Never EVER use $_GET in conjunction with file commands directly:

    isfile($_GET['p'].'.html')
    This can be abused by the most unknowledgeable hacker. While I won't get into proper filtering techniques, novices could use a switch statement:

    Code:
    switch( $_GET['p'] ){
        case 'file1':
           include( 'file1.html');
           break;
       //...
    
        default:
           include( 'default.html' );
    }

    }
    circlical - hosting software development
    forums * blog

  17. #17
    use the include() function

  18. #18
    never heard of the include() function gedevelopment
    could you give me some more info in that function? vbmenu_register("postmenu_3851379", true);

  19. #19
    ok, lets say you have a 1 page site. you have a php file and then use a html template to make life easier to edit the page visually. (in say frontpage etc)

    you would use the include() to put that html code into the page and display it.

    example:

    HTML TEMPLATE FILE: template.html

    <b> Hello And Welcome To This Test </b>

    PHP FILE: index.php


    <html>
    <head>
    <title>Some Title</title>
    </head>

    <body>
    <?php
    include ("template.html");
    ?>
    </body>
    </html>

    This will display: Hello And Welcome To This Test when the index.php file is loaded. Basically it put the HTML code into the php code. Makes visual editing of pages alot easier.

  20. #20
    thanks gedevelopment!
    I like your idea to use the include()
    and using that basic idea I found this on the web, it needs some adjustment, but I think it just might work:
    linker.php ( this page is to autolink textfiles in your directory)
    -------------
    <table border="1" bordercolor="#000000" align="center" WIDTH="75%"><tr
    bgcolor="#999999"><td><font
    color="#000000">Name</font></td><td><font color="#000000">Size</font></td></tr>

    <?php

    exec
    ("ls -la $dir",$lines,$rc);
    $count =
    count($lines) - 1;
    for (
    $i = 1; $i <= $count; $i
    ++) {
    $type =
    substr($lines[$i],0,1);
    $name =
    strrchr($lines[$i]," ");
    $name =
    substr($name,1);
    $dire =
    substr($lines[$i],0,strpos($lines[$i],$name));

    // echo "$dire</font>"; //
    if ($type == "d"
    ) {
    if (
    $name == "." or $name == ".."
    ) {
    ;
    } else {
    if (
    $dir == ""
    ) {
    $size=
    filesize($name);
    echo
    "<tr bgcolor="
    #999999"><td><a href="$name">$name</a></td><td>$size</td></tr>";
    } else {
    $size=
    filesize($name);
    echo
    "<tr bgcolor="
    #DDDDDD"><td><a href=$dir/disp?i=$name>$name</a></td><td>$size</td></tr>";
    }
    }
    } else {
    if (
    $dir == ""
    ) {
    $size=
    filesize($name);
    echo
    "<tr bgcolor="
    #999999"><td><a href=disp.php?i=$name>$name</a></td><td>$size</td></tr>";
    } else {
    echo
    "<tr bgcolor="
    #DDDDDD"><td><a href=$dir/?i=$name>$name</a></td></tr>";
    }
    }
    }
    ?>

    </table><br>

    disp.php (this page is to display the textfiles in tableized manner)
    -----------------
    <?php

    $text_dir
    ="/usr/home/jiwang/public_html/test";
    //the file path of your textfile
    $text_url="http://www.jiwang.org/test";
    //this is the url of your directory

    //change this below if you so desire...
    ?>

    <center><table BORDER CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="75%" BGCOLOR="#999999"
    bordercolor="#000000">
    <tr>
    <td><pre>
    <?php

    //of course you can add errors to this script. this will display them...
    function error($what
    ) {
    echo
    "<u>doc error...</u><p>n"
    ;
    echo
    $what
    ;
    exit;
    }

    if(!
    $i
    ) {
    echo
    "<title>docs</title>n</head><table><form action=$PHP_SELF><tr><td valign=top>docs: <input type=text name=i></td><td valign=top><input type=submit value=submit></form></td></tr></table>n"
    ;
    exit;
    }

    else {
    $i=
    addslashes($i); echo "<title>documents - $i</title>n</head>n";}

    $file=$text_dir."/".$i; $url=$text_url."/".$i
    ;

    if(
    is_file($file)) {

    include (
    "$file"
    );

    }

    else {
    error('file does not exist'
    );}

    ?>

    </pre>
    </td>
    </tr>
    </table></center>

  21. #21
    On the case of a absolutely necessary inclusion, like the one described by gedevelopment, a require() is a lot more adapted as the page would have no logical content without the html file.

  22. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Related Posts from theWHIR.com

Posting Permissions

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