簡體   English   中英

如何從PHP中的其他src文件夾加載文件

[英]How to load a file from a different src folder in php

我正在嘗試將html文件拉入並加載到textarea中(以后我可能會使用tinymce或ckeditor),但是如果它們與.php頁面加載到同一文件夾中,則它顯示文件。 我感覺好像丟失了一些東西,但是我需要能夠將它們從同一服務器和域上的其他文件夾中拉出。

我嘗試將完整路徑放在$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>

如果文件在另一個目錄中,則需要將其完整路徑放入文件中。 嘗試

// 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"

如果文件在目錄中但在不同的子文件夾中,請向上一級說,例如:

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

假設您當前已加載current-page.php,則可以使用如下相對路徑訪問文件:

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

只需確保將完整路徑添加到文件即可。

有關路徑的更多信息:

  1. 如何包含需要絕對路徑的PHP文件?
  2. 包括使用相對路徑和PHP的文件?

你為什么不試試這個

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

而不是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