如何更正JPATH_BASE从非root目录运行PHP脚本

时间:2016-11-16 19:01:19

标签: php joomla

我使用AcyMailing API脚本非常有效,但仅限Joomla root - 如http://example.com/api.php

但是如果我将相同的脚本放在同一个Joomla下的我自己的目录下,那么就说/scripts/api.php - 我有第12行的Apache错误

PHP Fatal error:  require_once(): Failed opening required '/var/www/html/joomla/scripts/includes/defines.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/joomla/scripts/api.php on line 12

令人困惑的代码部分是

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
 include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
 define('JPATH_BASE', dirname(__FILE__));
 require_once JPATH_BASE.'/includes/defines.php';
}

第12行是

 require_once JPATH_BASE.'/includes/defines.php';

如何修复路径以允许api.php不仅从Joomla root运行,而且从/scripts/api.php运行?

提前感谢任何想法尝试。

已编辑 - 这是推荐使用JPATH_SITE的完整脚本 - 但同样是错误

PHP Fatal error:  require_once(): Failed opening required 'JPATH_SITE/includes/defines.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/joomla/scripts/singlemail.php on line 12

下面的完整脚本可能会遗漏一些内容?

<?php

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
 include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
 define('JPATH_BASE', dirname(__FILE__));
 require_once JPATH_SITE.'/includes/defines.php';
}

require_once JPATH_SITE.'/includes/framework.php';
$app = JFactory::getApplication('site');


if(!include_once(rtrim(JPATH_ADMINISTRATOR,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_acymailing'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'helper.php')){
 echo 'This code can not work without the AcyMailing Component';
 return false;
 }
$mailer = acymailing_get('helper.mailer');
$mailer->report = true;
$mailer->trackEmail = true;
$mailer->autoAddUser = false;
$mailer->sendOne(11,'test@example.com');

?>

1 个答案:

答案 0 :(得分:0)

使用JPATH_SITE

例如:http://example.com/mysite/api.php

这里mysite是子文件夹,你在这里安装joomla那时候JPATH_BASE有问题。

例如: 1. http://example.com/mysite/api.php 2. http://example.com/api.php

以下路径将全部工作,因为JPATH_SITE将找到joomla安装文件夹。

require_once JPATH_SITE。“/ includes / defines.php”;