douxian6086 2014-05-01 19:36
浏览 458
已采纳

由于找不到文件,PHP自动加载失败

I am hosting on a GoDaddy shared hosting account. My absolute hosting path is:

/home/content/a/d/m/admwta/html/eqflow/

I have a directory structure like this:

eqflow
->api
   ->classes
     ->security
     ->utils
   ->v1

I have defined one class per file hosted in either the security or util directory. All files are all lower case and for the class names I followed the PEAR convention of _ to / so a file called getpasswordhash.php in the security directory directory has the name api_classes_security_getpasswordhash.

I have this autoload function:

function replaceunderscores ($classname) {
$path = str_replace('_', DIRECTORY_SEPARATOR, $classname);
$fullpath = "/home/content/a/d/m/admwta/html/eqflow/".$path.".php";

echo $fullpath . " 
";
if (file_exists($fullpath))  {

    require_once ($fullpath);
}
else {
    echo "could not find file 
";
}
}
spl_autoload_register('replaceunderscores');

when I call login.php, it always fails with this message

/home/content/a/d/m/admwta/html/eqflow/api/classes/security/getpasswordhash.php could not find file
Fatal error: Class 'api_classes_security_getpasswordhash' not found in /home/content/a/d/m/admwta/html/eqflow/api/v1/login.php on line 27

it is not passing the file_exists test in the autoload script, I don't know why? You can see in the echo statement I put to echo full path I am giving the full path of the file?

  • 写回答

1条回答 默认 最新

  • dongqian1925 2014-05-01 20:28
    关注

    I got this to work by changing the way I constructed the $fullpath in the autoload function. Rather than hard code the path, I used the $_SERVER["DOCUMENT_ROOT"] variable. So for me the path becomes:

    $fullpath =  $_SERVER["DOCUMENT_ROOT"]."/eqflow/".$path.".php";
    

    That works perfectly. I am unsure what the difference is between hardcoding the document root and using the server variable but using the server variable worked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题