简体   繁体   English

如何从PHP中的其他src文件夹加载文件

[英]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. 我正在尝试将html文件拉入并加载到textarea中(以后我可能会使用tinymce或ckeditor),但是如果它们与.php页面加载到同一文件夹中,则它显示文件。 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. 我尝试将完整路径放在$filename = ""但是如果它不在同一文件夹中,它将显示为空白。

<!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: 假设您当前已加载current-page.php,则可以使用如下相对路径访问文件:

$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? 如何包含需要绝对路径的PHP文件?
  2. Including files using relative paths with PHP? 包括使用相对路径和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()... ? 而不是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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