PHP fopen 文件名。它是相对的还是绝对的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1954992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP fopen filename. Is it relative or absolute?
提问by Dan
I'm having problems with my paths and fopen with reference to my web server.
我的路径和 fopen 有问题,参考我的 Web 服务器。
I have a file saved in public/dan/new/apps/lovescopes/thisfile.php.
我有一个文件保存在public/dan/new/apps/lovescopes/thisfile.php。
thisfile.php will create a new file in public/internalServer/lovescopes/xml/2009/12using fopen "x+".
thisfile.php 将使用 fopen "x+"在public/internalServer/lovescopes/xml/2009/12 中创建一个新文件。
Now my errors show in the line where fopen is:
现在我的错误显示在 fopen 所在的行中:
If I type in the path as relative like
../../../../internalServer/lovescopes/xml/2009/12I end up with a Permission Denied error.If I type an absolute path like
/public/internalServer/lovescopes/xml/2009/12I end up with "Failed to open Stream: No such File or Directory"
如果我以相对方式输入路径,就像
../../../../internalServer/lovescopes/xml/2009/12我最终会遇到 Permission Denied 错误一样。如果我输入一个绝对路径,比如
/public/internalServer/lovescopes/xml/2009/12我最终得到“无法打开流:没有这样的文件或目录”
I'm still confused if I should use relative or absolute paths. I have a ftp_nlist and it worked perfectly well with #2. Is Fopen the same?
我仍然很困惑我应该使用相对路径还是绝对路径。我有一个 ftp_nlist,它与 #2 配合得很好。Fopen是一样的吗?
Also with the different error messages which I believe points to the same path, I don't know which way am I doing it right 1 or 2?
还有我认为指向相同路径的不同错误消息,我不知道我是正确的 1 还是 2?
回答by Gordon
Why not use use realpath()with your relative paths.
为什么不将realpath()与您的相对路径一起使用。
Also permission deniedindicates that the folder you are trying access to is not readable and/or writable by the webserver or whatever user your PHP process runs under.
此外,权限被拒绝表示您尝试访问的文件夹无法被网络服务器或您的 PHP 进程在其下运行的任何用户读取和/或写入。
In addition, if you want to read/write files in one go from/to a variable, you might want to use file_get_contents()and file_put_contents().
此外,如果您想一次性从/向变量读取/写入文件,您可能需要使用file_get_contents()和file_put_contents()。
回答by ssube
The PHP function itself is neither absolute nor relative, that depends on the path.
PHP 函数本身既不是绝对的也不是相对的,这取决于路径。
Both the errors you're getting are related to that specific path, either permissions or existence, so I'd recommend getting a path you know exists and you have access to and trying that (the current directory may be a good place). You may also temporary chmod the file and path, just to make sure you have read access. Print the results and any errors.
您遇到的两个错误都与该特定路径有关,无论是权限还是存在,因此我建议您获取一个您知道存在的路径并且您可以访问并尝试该路径(当前目录可能是一个好地方)。您还可以临时修改文件和路径,以确保您具有读取权限。打印结果和任何错误。
fopen("public/dan/new/apps/lovescopes/test.php");
fopen("./test.php");
If neither of those return errors, you'll know it was just a path issue.
如果这些都没有返回错误,您就会知道这只是一个路径问题。
Also, your PHP process may have different permissions than your user, so watch out for that.
此外,您的 PHP 进程可能具有与您的用户不同的权限,因此请注意这一点。
回答by user2089563
Also, make sure that you're relative path is specified relative to the file where the function is being called (not necessarily where the actual code is).
In PHP use getcwd()(get current working directory; manual) to figure out which directory to base your relative path off of.
此外,请确保您指定的相对路径是相对于调用函数的文件(不一定是实际代码所在的位置)。
在 PHP 中,使用getcwd()(get current working directory; manual) 来确定您的相对路径基于哪个目录。
回答by Carson Myers
You have to make sure your script has permission to access that file
您必须确保您的脚本有权访问该文件

