我正试图将一个html文件拉入并加载到文本区域(稍后我可能会在其中使用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"
只需确保将完整路径添加到文件中即可。
更多路径:
- 如何包含需要绝对路径的PHP文件
- 使用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>