如何轻松地从不同的文件夹位置映射文件路径? [英] How to map file path from different folder location easily?

查看:222
本文介绍了如何轻松地从不同的文件夹位置映射文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何轻松地将路径映射到文件?

how to map the path to the file easily?

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

the actual path of the text file is in public_html/r/admin/logfile/testing_randomstring.txt

因此,如果我在public_html/folder1/folder2/addlog.php上运行脚本,它将无法找到testing_randomstring.txt

so if I run the script at public_html/folder1/folder2/addlog.php, it won't be able to find the path to the testing_randomstring.txt

addlog.php
<?php

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

writetologfile('hahaha');

?>

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

How I can able to easily point to this text file path, no matter where my php calling script is from.

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

I tried to change $filename = 'logfile/testing_randomstring.txt'; inside writetologfile function by enforcing it to absolute fix path, something like $filename='/r/admin/logfile/testing_randomstring.txt', but it is not working

推荐答案

您可以指定绝对路径,而不是使用相对路径.假设public_html在您的主目录中,请尝试以下操作:

Instead of using a relative path, you could specify an absolute path. Assuming public_html is in your home directory, try this:

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

这使用 getenv 来读取环境变量$HOME.

这篇关于如何轻松地从不同的文件夹位置映射文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆