php fopen相对路径断开-神秘 [英] php fopen relative path broken - mystery

查看:77
本文介绍了php fopen相对路径断开-神秘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道必须进行某些更改",但是我的代码似乎整夜无缘无故被破坏.

I know "something must have been changed" but my code seems to have broken over night for no reason.

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

My server directory structure is something like this:

/
/脚本
/审核
/other_things

/
/scripts
/audit
/other_things

我在"scripts"文件夹中有一个脚本(假设它称为"/scripts/MyScript.php"),该脚本使用curl收集网页数据,并在审核"中保存所读取网页的带日期的副本. 文件夹.

I have a script (let's say it's called "/scripts/MyScript.php") in the "scripts" folder which gathers data from a webpage using curl, and saves a dated copy of the webpage it reads in the "audit" folder.

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

To write to the audit folder, I used

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

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

无论如何停止工作,都会抛出

however that stopped working, throwing

[function.fopen]:无法打开流:在第353行的/home/web/website.co.uk/audit/2008-09-09-183000.backup.log中没有此类文件或目录

[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

不过,我通过将路径更改为

I have however fixed this by changing the path to

"../audit/2008等."来自"./audit/2008"(这是两个句号/句号,而不是一个)

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

逻辑表明服务器配置必须有所更改,但是怎么办?这是我管理的专用服务器.我如何避免再次发生这种情况?

Logic dictates that something must have changed in the server configuration, but what? It is a dedicated server which I manage. How can I avoid something like this happening again?

我什至通过了MyScript.php的SVN,所有以前的版本都使用了single.在路径中.

I've even gone through SVN for MyScript.php and all previous versions have used the single . in the path.

推荐答案

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

Use dirname(__FILE__) to get the filesystem path for the current file. Then use relative paths from there to locate your audit directory.

例如,在scripts/MyScript.php中,dirname(__FILE__)将返回/home/web/website.co.uk/scripts.您可以将/../audit可靠地附加到其上.

For example, within scripts/MyScript.php, dirname(__FILE__) will return /home/web/website.co.uk/scripts. You can reliably append /../audit to that.

(请注意,这甚至可以在include d或require d文件中使用,在这种情况下,它将返回包含文件所在的目录).

(Note this even works in an included or required file—in that case it will return the directory in which the included file is located).

这篇关于php fopen相对路径断开-神秘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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