简体   繁体   中英

How to load a file from a different src folder in php

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>

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?

Why don't U try this

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

instead of fopen()... ?

尝试这个:

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM