在drupal7模块中,使用file_put_contents函数保存文件时,遇到文件路径如何使用的问题,测试了下面的一些用法,供大家参考。 测试方式: 通过menu的回调函数执行保存文件的操作 测试案例 在回调函数中执行getcwd()函数,获取当前工作目录

echo getcwd();# /var/www/project/demosite 网站的目录所在Ubuntu中的绝对路径

当前文件所在目录

echo __DIR__;# /var/www/project/demosite/sites/all/modules/customized_form_ui 当前模块所在的绝对路径

当前文件名

echo __FILE__; # /var/www/project/demosite/sites/all/modules/customized_form_ui/customized_filetest_page.inc 当前menu的回调函数所在目录

下面我们测试写入文件

$write = file_put_contents('test.txt','123456'); # 文件保存在了网站的根目录下
$write = file_put_contents('sites/all/modules/customized_form_ui/test.txt','123456'); # 文件保存在模块的根目录下
$write = file_put_contents('sites/all/modules/customized_form_ui/templates/images/test.txt','123456'); # 文件保存在模块的文件夹下

注:上面代码仅仅是测试,我们在实际的开发中,应该将文件保存在 sites/all/default/files/ 的某个文件夹下,方便模块的管理 更改当前工作目录

chdir('sites/all/modules/customized_form_ui/');
echo getcwd(); # /var/www/project/demosite/sites/all/modules/customized_form_ui

drupal7中开发中用的一些path相关的函数 current_path();//当前路径 global $base_path; //网站根目录 / global $theme_path; //当前主题路径 path_to_theme();//当前页面的主题路径 可能是theme或者module path_load();// 从数据库中获取url地址的别名 drupal_get_path(); //获取module theme等路径


上述代码为个人测试,仅供参考。