简体   繁体   English

PHP的require()不适用于相对或完整

[英]php require() does not work with relative or full

I have a weird problem with the require() command, it does not work at all and gives me a weird error.. yes i am 100% sure i put the right path i tried using both relative and full paths, as well as using include() , include_once() , require() , require_once() but to no avail.. I tested the file's existence and it does exist.. it is making no sense to me! 我的require()命令有一个怪异的问题,它根本不起作用,给我一个怪异的错误。是的,我100%肯定我使用相对和完整路径以及使用了正确的路径include()include_once()require()require_once()但无济于事。我测试了文件的存在并且它确实存在..对我来说这没有意义! I haven't seen anyone else having the same problem as this on forum, I also tried using global variables like $_SERVER["DOCUMENT_ROOT"] and other paths but this still doesn't work for me. 我在论坛上没有看到其他人遇到过与此相同的问题,我也尝试过使用$_SERVER["DOCUMENT_ROOT"]类的全局变量和其他路径,但这对我仍然不起作用。

The procedure is: HTML button -> ajax calls "addcomments.php" -> page requires "ajax.php"-> error 过程是:HTML按钮-> ajax调用“ addcomments.php”->页面需要“ ajax.php”->错误

addcomments.php page: addcomments.php页面:

//set path
$path = "g:/appserv/www/y4project/scripts/php/funcs/";

//check directory 
print_r(scandir($path));

//check if php file exists
if(file_exists($path."ajax.php"))
    echo "######### IT EXISTS!!! ##########";
else echo "@@@@@@ NO! @@@@@@@@";

//require it
require($path."ajax.php");

The error: 错误:

Array ( 
    [0] => . 
    [1] => .. 
    [2] => account.php 
    [3] => ajax.php 
    [4] => main.php 
    [5] => service.php ) 

######### IT EXISTS!!! ##########

Warning: require(1) [function.require]: failed to open stream: No such file or directory in G:\AppServ\www\y4project\scripts\AJAX\addcomments.php on line 10

Fatal error: require() [function.require]: Failed opening required '1' (include_path='.;C:\php5\pear') in G:\AppServ\www\y4project\scripts\AJAX\addcomments.php on line 10

I also tried relative paths like "../php/funcs/ajax.php", same problem.. scandir() and file_exists() both find it but require() fails! 我也尝试过相对路径,例如“ ../php/funcs/ajax.php”,同样的问题。scandir scandir()file_exists()都找到了,但是require()失败了!

Your help is most appreciated! 非常感谢您的帮助!

EDIT 编辑

I just tested it to be sure, required it from a different test page and used a function and it worked, but still not working from the "addcomments.php" page which gets called by AJAX. 我只是对其进行了测试,以确保可以从其他测试页面中使用它,并且使用了一个函数并且可以正常工作,但是仍然无法通过AJAX调用的“ addcomments.php”页面正常工作。

Another EDIT 另一个编辑

I also checked the documentation for the PHP require_once() and require() , I still can't see the problem! 我还检查了PHP的文件require_once()require() ,但仍然看不到问题!

Could it be a path bug of some sort or a security measure where AJAX called scripts cannot call other scripts what so ever? 可能是某种路径错误,还是一种安全措施,使得称为脚本的AJAX无法如此调用其他脚本? but that makes no sense? 但这没有意义吗?
I did not read anything about that in the documentation either. 我也没有在文档中阅读有关该内容的任何内容。
I even tried requiring HTML pages and still not working. 我什至尝试要求HTML页面,但仍然无法正常工作。

I'm lost.. 我迷路了..

您可以尝试使用这种方式:

require dirname(__DIR__) . "/php/funcs/ajax.php";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM