douzao2992 2014-05-27 01:08
浏览 100
已采纳

在codeigniter安装之外从外部php脚本访问CodeIgniter超级对象

I have been trying so long but couldn't find a solution. For some reason I need to access the codeigniter super object get_instance() from an external php script which is located outside codeigniter installation.

So for example, I have a custom php script called my_script.php inside public_html. And the codeigniter is installed inside public_html/codeigniter.

Followed by the discussion here: http://ellislab.com/forums/viewthread/101620/ I created a file called external.php and put it inside public_html/codeigniter folder, it contains the following code:

<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
ob_start();
require('./new/index.php');
ob_end_clean();
?>

Then I created a file called my_script.php and put it inside public_html folder (outside codeigniter installation), it contains the following code:

<?php
require('new/external.php');
$ci =& get_instance();
echo $ci->somemodel->somemethod();
?>

Now when I load the my_script.php file from browser, it produces following error:

Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php

If I put the my_script.php file inside codeigniter folder with corrected file path in require() function then it works. But I really need it to work from outside codeigniter installation.

Any idea how to get rid of this problem?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • drtppp75155 2014-05-27 02:13
    关注

    CI's main index.php sets the paths of the system and application folders. If you are including index.php from another directory, these paths are set relative to your "including" directory.

    Try changing the following lines in index.php

    $system_path = 'system';
    // change to...
    $system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system';
    
    // and...
    $application_folder = 'application';
    // change to...
    $application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application';
    

    The dirname(__FILE__) will give you the absolute path of the index.php even if you include it somewhere else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题