【问题标题】:How to map file path from different folder location easily?如何轻松映射来自不同文件夹位置的文件路径?
【发布时间】:2009-10-16 10:16:22
【问题描述】:

如何轻松映射文件路径?

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);    
}

?>

文本文件的实际路径在public_html/r/admin/logfile/testing_randomstring.txt

所以如果我在public_html/folder1/folder2/addlog.php 运行脚本,它将无法找到testing_randomstring.txt 的路径

addlog.php
<?php

include("../../api/function.php");

writetologfile('hahaha');

?>

无论我的 php 调用脚本来自何处,我如何能够轻松地指向此文本文件路径。

我试图改变 $filename = 'logfile/testing_randomstring.txt';在 writetologfile 函数中,通过将其强制为绝对修复路径, 类似于 $filename='/r/admin/logfile/testing_randomstring.txt', 但它不起作用

【问题讨论】:

    标签: php file path map


    【解决方案1】:

    您可以指定绝对路径,而不是使用相对路径。假设 public_html 在你的主目录中,试试这个:

    $filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
    fopen(getenv('HOME') . $filename, 'a');
    

    这使用getenv来读取环境变量$HOME的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多