Drupal 6 Drupal 6:在hook_cron中,如何指定模块文件的路径?

Drupal 6 Drupal 6:在hook_cron中,如何指定模块文件的路径?,drupal-6,drupal-modules,Drupal 6,Drupal Modules,在我的.module文件中,我有一个hook_cron的基本实现: function foobar_cron() { $file = fopen('my_file', 'a'); // stuff fclose($file); } function foobar_cron() { $file = fopen(realpath(".") . PATH_SEPARATOR . drupal_get_path('module', 'foobar') . PAT

在我的.module文件中,我有一个hook_cron的基本实现:

function foobar_cron()
{
    $file = fopen('my_file', 'a');
    // stuff
    fclose($file);
}
function foobar_cron()
{
        $file = fopen(realpath(".") . PATH_SEPARATOR . drupal_get_path('module', 'foobar') . PATH_SEPARATOR . 'myfile.txt', 'a');
}
问题是这个方法是由(http://www.example.com/)因此my_文件的路径不正确如何为位于foobar模块目录中的\u文件指定正确的路径?


<?php
   // something like the following. Might need to tweak the pathing.
   $path = drupal_get_path('module', $module_name) . '/my_file'; // $module_name = foobar in your case 
?>

我发现在实现hook\u cron()时,可以使用以下代码访问模块目录中的文件:


路径分隔符确保该路径也在Windows上工作。

umm,这取决于服务器的配置方式,该服务器应返回字符串路径,如/sites/all/modules/foobar/myfile,相对于htdocs。因此,您可以修改它并手动执行如下操作:$path='/var/ww/htdocs'。drupal_get_路径('module','foobar')。'/我的档案$file=fopen($path,'a');应该可以,但这正是您想要的,因为Drupal的当前目录始终是顶级目录,因为所有请求都通过index.php、cron.php或update.php。因此,直接使用$path是安全的。Drupal本身也在做同样的事情。路径分隔符是“:”或“;”,而不是/。你的意思是目录分隔符。此外,简单地使用/也是非常安全的,因为它总是有效的,包括Windows。