fopen 在普通 PHP 中工作,但在 Drupal 中不起作用
一些 PHP 代码运行良好。但是,当我将代码移植到 Drupal 模块中时, $fp = fopen( 'language/unicode-big5.tab', 'r' );
无法按预期工作,并发出警告:
warning: fopen(language/unicode-big5.tab) [function.fopen]: failed to open stream: No such file or directory in /Users/me/Documents/Html/drupal/sites/all/modules/test_module/language.inc.php on line 197.
我尝试将所需的文件和文件夹放入 sites/default/files
中:
$fp = fopen( file_directory_path().'/language/unicode-big5.tab', 'r' );
返回相同的警告。
我该如何修复它?
Some PHP code worked well. However, when I port the code into a Drupal module, the $fp = fopen( 'language/unicode-big5.tab', 'r' );
does not work as expect, giving a warning:
warning: fopen(language/unicode-big5.tab) [function.fopen]: failed to open stream: No such file or directory in /Users/me/Documents/Html/drupal/sites/all/modules/test_module/language.inc.php on line 197.
I tried to put the required files and folder in sites/default/files
:
$fp = fopen( file_directory_path().'/language/unicode-big5.tab', 'r' );
returns the same warning.
How can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那会工作得很好。
然而,该消息只是表明您的路径是错误的,您是否尝试过简单地查看它试图打开的路径?
只需执行以下操作,并相应地更正路径即可。
如果路径正确,那么您需要检查权限,该用户是否有权访问该文件?尝试 chmod 777 并查看是否可以解决问题,如果可以,请删除 chmod 777 并相应修复它(用户/组权限)。
That will work perfectly fine.
However the message is simply stating that your path is wrong, have you tried a simply looking at what path it's trying to open?
Just do the following, and correct the path accordingly.
If the path is correct, then you need to check permissions, does that user have access to the file at all? Try a chmod 777 and see if that helps the issue, if it does, remove the chmod 777 and fix it accordingly (user/group permissions).
最好使用遵循 drupal 标准的模块路径并将其添加到文件的路径中:
drupal_get_path('module', $module_name);
Its better to use module path following drupal standards and add that to the path of the file :
drupal_get_path('module', $module_name);
您的 Drupal 是否在与您所引用的文件不同的所有者下运行?文件可读吗? (chmod +r 文件)
Is your Drupal running under a different owner than the file you are referring to? Is the file readable? (chmod +r file)
在 Drupal 7 和 8 中,有
drupal_realpath()
。文档中不鼓励使用此函数。
https://api.drupal.org/api /drupal/includes!file.inc/function/drupal_realpath/7.x
In Drupal 7 and 8, there is
drupal_realpath()
.Use of this function is discouraged in the documentation.
https://api.drupal.org/api/drupal/includes!file.inc/function/drupal_realpath/7.x