2

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! 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.

The procedure is: HTML button -> ajax calls "addcomments.php" -> page requires "ajax.php"-> error

addcomments.php page:

//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!

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.

Another EDIT

I also checked the documentation for the PHP require_once() and require(), I still can't see the problem!

Could it be a path bug of some sort or a security measure where AJAX called scripts cannot call other scripts what so ever? 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.

I'm lost..

bakz
  • 79
  • 15
  • Have you tried with \ (instead of / ) and correct upper/lower cases? For example change `appserv` with `AppServ` as it's named in your computer. So to have something like this: `require("G:\AppServ\www\y4project\scripts\php\funcs\ajax.php");` – Daniel Dudas Aug 16 '16 at 15:58
  • file_exists() isn't particularly useful. try `is_readable()` instead. file_exists will tell you there's a "free, take one" basket inside the locked bank vault, but that doesn't mean you can actually go in to take that free thing. – Marc B Aug 16 '16 at 15:59
  • @DanielDudas I did, nothing.. – bakz Aug 16 '16 at 16:00
  • 3
    It's weird that the filename is supposedly "1"... what is converting that string into a number? Try single quotes or leaving the brackets (they are not needed as `require` is a language construct and not a function). – YetiCGN Aug 16 '16 at 16:00
  • @MarcB just tested it, it returned true – bakz Aug 16 '16 at 16:00
  • @DanielDudas: it's a windows path. windows filenames aren't case sensitive, and swapping to backslashes will also fail if any of the characters AFTER the backslash would be metachars, e.g. `file_exists("c:/noodles.txt")` is ok, but `file_exits("c:\noodles.txt")` ISN'T, because `\n` is a newline – Marc B Aug 16 '16 at 16:00
  • You're not - by any chance - require'ing the result of `file_exists`? Which would be true and cast to 1? It's not in the code you posted but that's the only reason I can think of without hands-on debugging. @MarcB: But you can escape the backslashes with backslashes. ^^ – YetiCGN Aug 16 '16 at 16:03
  • @YetiCGN: true, but daniel didn't do that, so just blindly swapping slash types wouldn't magically fix things. – Marc B Aug 16 '16 at 16:04
  • @YetiCGN the exact code is what i posted in the question, i am setting the whole path yet nothing! – bakz Aug 16 '16 at 16:07
  • @bakz: Please post your webserver stack and PHP version. The only way I can get the error you describe is if I deliberately try to `require(1)` or any other expression that evaluates to `true`. – YetiCGN Aug 16 '16 at 18:23
  • @bakz: This is not the "*exact* code", because you're missing the PHP open tag and line 10 does not contain a require statement, it's on line 13. Are you sure you don't have some other version of `addcomments.php` in your project that you're editing and that's why you don't see any changes? – YetiCGN Aug 16 '16 at 18:28
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Aug 20 '16 at 08:18

1 Answers1

0

You can try to use this way:

require dirname(__DIR__) . "/php/funcs/ajax.php";
Rafal Kozlowski
  • 720
  • 5
  • 12
  • nope, did not work.. also tried "./../" as well, did not work... I could bet a on it that the AJAX call is causing something, I still can't figure it out what it is yet.. – bakz Aug 16 '16 at 18:18
  • Then try to open addcomments.php directly in the browser without AJAX to test that hypothesis. – YetiCGN Aug 16 '16 at 18:24
  • Can You share a result of `var_dump(__DIR__);` from both files? – Rafal Kozlowski Aug 16 '16 at 18:48
  • @YetiCGN I did, it works when i call it like from the web browser, but the require fails when the AJAX is involved, i did state everything in the main post – bakz Aug 17 '16 at 15:10
  • 1
    "When the (sic!) AJAX is involved" there is nothing differently handled on the backend than opening the page in a browser or using cURL or wget or similar on the command line. Open Developer Tools in Chrome and inspect the Network tab to see what's going over the wire and compare the two. On another note: Are you sure you don't have some other version of `addcomments.php` in your project that you're editing and that's why you don't see any changes? – YetiCGN Aug 17 '16 at 15:50
  • +YetiCGN Nope... only one there, nowhere else in the whole web circuitry has that name.. restart didn't work, I'm reinstalling the whole thing today to see, just incase.. – bakz Aug 21 '16 at 12:31