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 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私