Php 如何从不同的文件夹位置轻松映射文件路径?
如何轻松地将路径映射到文件Php 如何从不同的文件夹位置轻松映射文件路径?,php,file,path,map,Php,File,Path,Map,如何轻松地将路径映射到文件 public_html/api/function.php <?php function writetologfile($content) { $filename = 'logfile/testing_randomstring.txt'; if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit;
public_html/api/function.php
<?php
function writetologfile($content)
{
$filename = 'logfile/testing_randomstring.txt';
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
fclose($handle);
}
?>
无论我的php调用脚本来自何处,我都可以轻松地指向这个文本文件路径
我试图更改$filename='logfile/testing_randomstring.txt';在writetologfile函数中,将其强制为绝对固定路径,
类似于$filename='/r/admin/logfile/testing_randomstring.txt',
但是它不起作用您可以指定一个绝对路径,而不是使用相对路径。假设
public\u html
在您的主目录中,请尝试以下操作:
$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');
这用于读取环境变量$HOME
的内容
$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');