1

I am trying to pull and load an html file into a textarea (that I will later use tinymce or ckeditor on probably) however it ONLY shows files if they are loaded into the same folder as the .php page. I feel like I am missing something but I need to be able to pull them from other folders on the same server and domain.

I've attempted putting in the full paths in $filename = "" however if it isn't in the same folder it just appears blank.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <textarea cols="30" rows="10">
        <?php
        $filename = "../projectevo/new-page-6.html";
        $handle = fopen($filename, "r");
        $contents = fread($handle, filesize($filename));
        fclose($handle);

        echo $contents;

        ?>
        </textarea>
    </body>
</html>
user3518979
  • 57
  • 2
  • 7

3 Answers3

1

You need to put the full path to the file if it's in another directory. Try

// Get the Base Path to your website
$basePath = dirname(__FILE__);  

// Now use the complete path to the file
$filename = $basePath . "/path/to/file/new-page-6.html"

If the files are in a directory but in differen subfolders, say one level up, for example:

-main_directory
--dirA
---current-page.php
--dirB
---new-page-6.html

Assuming you currently have current-page.php loaded, you can access the file using a relative path like:

$filename = "../dirB/new-page-6.html"

Just make sure you're adding the full path to your file.

More on paths:

  1. How to include PHP files that require an absolute path?
  2. Including files using relative paths with PHP?
Community
  • 1
  • 1
Joshua Kissoon
  • 3,234
  • 6
  • 30
  • 57
  • 1
    Hey Joshua, thanks for this. Its really weird, I had tried the relative file path earlier, and now it seems to be opening but at times, like when I test with another path that might be wrong, then go back and save this one, I try and open it, it appears blank. Then a few minutes later I'll go back and load the page again and it will work. basically meaning even when I have it right, it doesn't always load, any clue? On the rest, I guess I just missed writing the relative path name wrong. Thank you again. – user3518979 Apr 24 '14 at 03:59
0

Why don't U try this

$path = '../test.txt';
//$path = 'test.txt';
//$path = '/home/test.txt';
echo file_get_contents($path);

instead of fopen()... ?

endoffight
  • 15
  • 4
0

Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <textarea cols="30" rows="10"> <?php $filename = $_SERVER['DOCUMENT_ROOT']."/any folder in your server/any other folder/filename.html"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); echo $contents; ?> </textarea> </body> </html>