0

I try to include PHP file from different places of my computer. So I have defined different include paths in my php.ini

include_path=".;C:\project\a;X:\project\b"

When I try to include a file X:\project\b\file_b.php from a PHP script on C:\project\a\file_a.php with

require_once("file_b.php");

or with

require_once("X:\project\b\file_b.php");

I get the error

Warning: require_once(X:\project\b\file_b.php): failed to open stream: No such file or directory in C:\project\a\file_a.php on line 2

Fatal error: require_once(): Failed opening required 'X:\project\b\file_b.php' (include_path='.;C:\project\a;X:\project\b') in C:\project\a\file_a.php on line 2

Abort Processing during Fatal-Error: require_once(): Failed opening required 'X:\project\b\file_b.php' (include_path='.;C:\project\a;X:\project\b') Error in Script C:\project\a\file_a.php on Line 2

I have tried everything I can do with my knowlegde, but nothing works. I hope, that someone can hlep me with solving this problem. Is there a problem with include paths on different drives on a Windows system?

Best regards, bition

Community
  • 1
  • 1
bition
  • 1
  • 5
  • does whatever user ID your webserver is running under have the rights to access that drive/folders/file? and note that it's a good idea to NOT use backslashes for paths in php. `file_get_contents("C:\now\then\forever")` is not reading a file "forever" in the "now\then" subdirectory, it's reading `c:[linefeed]ow[tab]hen[formfeed]orever` – Marc B Sep 16 '16 at 19:26
  • You can try `chdir` before the `require_once` instruction....http://php.net/manual/en/function.chdir.php – Hackerman Sep 16 '16 at 19:27
  • Thank you for your comments. @Marc B: the files which should be included are accessible by everone. So I think there is no peermission problem with the files. – bition Sep 16 '16 at 21:14
  • @Hackerman: chdir() ist also not working and PHP says "chdir(): No such file or directory (errno 2)" – bition Sep 16 '16 at 21:16
  • Have you tried the below answer? – ZBerg Sep 16 '16 at 22:57
  • If you're using double quotes `"`, you need to escape backslashes with another backslash. So, use `\\`. The error message seems to indicate that this isn't affecting you right now, but take a look. – Brad Sep 19 '16 at 02:21
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 22 '16 at 07:58

2 Answers2

0

After a long time, I have finally the answer for my special problem.

It is really ery special, because I use BoxCryptor to encrypt my D: drive. BoxCryptor creates the virtual device X:, but it is not present for Apache and PHP. But I can configure BoxCryptor the mount the device X: as a rwal harddrive. After I set this, everythings works fine.

Sorry for the confusion about my vortual and real harddrives and thank you for your help!

Best regards, bition

bition
  • 1
  • 5
-1

I have found with WAMP etc in the past, that using backslashs works. Just dont encapsulate the string with double quotes "C:\path", this will process the contents as @Marc B warns. Instead encapsulate the string with single quotes 'C:\path' and then the contents are not processed.

If the backslashes in the contents of the single quote string are still being processed, try the following.

<?php

$path = 'X:/project/b/file_b.php';            // PATH WITH FORWARD SLASHS
$path = str_replace(chr(47) ,chr(92) ,$path); // REPLACE FORWARD WITH BACKWARD SLASHS
require_once($path);                          // RUN FILE

?>
ZBerg
  • 78
  • 6
  • The string replace is completely unnecessary. Just use `\\ `. Also, it shouldn't be a problem in the first place if you use single quotes. – Brad Sep 19 '16 at 02:22
  • I'm not sure thats entirely accurate, i have had issues in the past on windows systems with forward slashes in local paths. I did mention quote encapsulations in the notes before the code example. – ZBerg Sep 19 '16 at 02:28
  • Then don't use forward slashes... use back slashes. The point is, you can use the backslashes in your string literal. No need for the replacement. – Brad Sep 19 '16 at 02:29
  • Downvote my answer, and include it in your own... Seems abit unfair that... Especailly after the correct answer was given. If the two correct methods dont wory, try this... downvote :S – ZBerg Sep 19 '16 at 02:31
  • I downvoted your answer because what you are doing is completely unnecessary and doesn't really answer the question. If you correct your answer, I'd happily upvote it. And, I don't know why you think suggesting fixing quotes in a comment is somehow including your answer in mine? I didn't even answer the question... I left a comment with the solution. – Brad Sep 19 '16 at 02:33
  • **Just dont encapsulate the string with double quotes "C:\path", this will process the contents as @Marc B warns. Instead encapsulate the string with single quotes 'C:\path' and then the contents are not processed.** – ZBerg Sep 19 '16 at 02:34
  • PHP will convert to OS-specific directory separators for you. So just use forward slashes for everything and let PHP do the worrying for you. The **ONLY** time you'd have to use backslashes if you're passing paths to external things, e.g. `exec('c:\windows\system32\cmd.exe /c blah blah blah')` - PHP doesn't process those, the OS does. – Marc B Sep 19 '16 at 15:20
  • More generally you can quickly find the cause of this problem by following this checklist : http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Sep 22 '16 at 07:59