gpt4 book ai didi

php fopen 相对路径损坏 - 神秘

转载 作者:行者123 更新时间:2023-12-01 22:47:36 28 4
gpt4 key购买 nike

我知道“一定有什么东西被改变了”,但我的代码似乎在一夜之间无缘无故地崩溃了。

我的服务器目录结构是这样的:

/
/scripts
/audit
/other_things

我在“scripts”文件夹中有一个脚本(假设它名为“/scripts/MyScript.php”),它使用curl从网页收集数据,并将其读取的网页的日期副本保存在“audit”中“文件夹。

要写入审核文件夹,我使用了

$fh = fopen("./audit/2008-09-09-183000.backup.log","w");

然而,这停止工作,抛出

[function.fopen]: failed to open stream: No such file or directory in /home/web/website.co.uk/audit/2008-09-09-183000.backup.log on line 353

但是,我通过将路径更改为

解决了这个问题

"../audit/2008 etc." from "./audit/2008" (that's two full stops/periods, instead of one)

逻辑表明服务器配置中一定发生了某些变化,但是什么?这是我管理的专用服务器。我怎样才能避免类似的事情再次发生?

我什至已经通过 SVN for MyScript.php 并且所有以前的版本都使用了单个 .在路径中。

最佳答案

使用dirname(__FILE__)获取当前文件的文件系统路径。然后使用相对路径从那里找到您的 audit目录。

例如,在scripts/MyScript.php内, dirname(__FILE__)将返回/home/web/website.co.uk/scripts 。您可以可靠地附加 /../audit对此。

(请注意,这甚至适用于 include d 或 require d 文件 - 在这种情况下,它将返回包含文件所在的目录)。

关于php fopen 相对路径损坏 - 神秘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1303199/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com