don*_*che 3 php cron drupal php-include drupal-7
我在Drupal 7中编写了一个表单,我想'包含'一个php文件,它将我的数据库连接变量添加到我的代码中.
我已经用几种方式测试了我的代码,我确信当我在我的网站中运行Cron时,'include'会让我获得一个WSOD.
我用Google搜索了这个并尝试过:
include("/sites/all/libraries/php/connection.inc");
include("./sites/all/libraries/php/connection.inc");
include"./sites/all/libraries/php/connection.inc";
Run Code Online (Sandbox Code Playgroud)
我也用'.php'扩展了上面的内容.
我最后一次尝试:
define('__ROOT__', dirname(dirname(__FILE__)));
include(__ROOT__.'/sites/all/libraries/php/connection.inc');
Run Code Online (Sandbox Code Playgroud)
有时我在尝试运行Cron时会得到一个WSOD,有时我提交表单时我的页面样式会被破坏.
在Drupal中手动包含PHP文件的正确方法是什么?请注意,我不想使用'Drupal方式'来执行此操作或使用webform模块.我想用PHP手动编写代码.
sch*_*cki 10
libraries目录应仅用于存储通过库模块作为库提供的文件(请参阅此处https://drupal.org/project/libraries).
对于这两个示例,我们假设library.inc是我们的文件,relative_path_to是基于library.inc文件的模块目录的相对路径.
要包含您可以使用的文件:
require(drupal_get_path('module', 'module_name') . '/relative_path_to/library.inc');
Run Code Online (Sandbox Code Playgroud)
并以Drupal的方式(https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7):
module_load_include('inc', 'module_name', '/relative_path_to/library');
Run Code Online (Sandbox Code Playgroud)
干杯,j
小智 5
试试这种方式
1) require 'includes/iso.inc';
2) include_once 'includes/iso.inc';
3) include ('includes/iso.inc');
Run Code Online (Sandbox Code Playgroud)
或Drupal方式
include_once DRUPAL_ROOT . '/includes/iso.inc';
Run Code Online (Sandbox Code Playgroud)
在我看来,这些是正确的Drupal(7)包含代码的方法:
module_load_include();函数 - Drupal API文档 - 在您需要的模块中包含任何PHP文件,files[] = ...在your_module.info文件中自动加载包括你自己模块中的类和接口 - 编写.info文件文档.库模块提供了自己的方式来包含库中的外部PHP文件.