CloudWindMoonSun Posted November 29, 2017 Share Posted November 29, 2017 Hi, As beginner to PHP, I tried everything I know to include a php file which is in another folder, but I had no success. So the folder structure I use is as following: MyFolder (folder) | |--- phpseclib (folder) | | | |--- Crypt (folder) | | | |--- RSA.php | |--- foo.php In foo.php, which its contents is listed below, I need to include RSA.php, which resides in MyFolder/phpseclib/Crypt/ but it just doesn't work. What am I doing wrong? <?php namespace MyOwnNamespace; include('phpseclib/Crypt/RSA.php'); // require_once 'phpseclib/Crypt/RSA.php'; // even this doesn't work class MyClass { public function MyFunction() { $rsa = new \Crypt_RSA(); $rsa->setHash("sha256"); ... } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/ Share on other sites More sharing options...
dodgeitorelse3 Posted November 29, 2017 Share Posted November 29, 2017 (edited) using namespace caused white page for me, will have to look it up Edited November 29, 2017 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554250 Share on other sites More sharing options...
CloudWindMoonSun Posted November 30, 2017 Author Share Posted November 30, 2017 Thanks for looking in to this. But even without defining "namespace MyOwnNamespace;" on the top, it fails to instantiate Crypt_RSA and setHash() of it. Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554251 Share on other sites More sharing options...
BigB Posted November 30, 2017 Share Posted November 30, 2017 In regards to "white page" Create a page - ie my_error_find.php and include the file you are testing to see if you can jump a useful error out. <?php ######### ERROR FINDING PAGE ######### ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('error_reporting', E_ALL); error_reporting(E_ALL); error_reporting(-1); include("phpseclib/Crypt/RSA.php"); // <<== PATH TO FILE WITH ERRORS include("foo.php"); // <<== PATH TO FILE WITH ERRORS ?> Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554252 Share on other sites More sharing options...
maxxd Posted November 30, 2017 Share Posted November 30, 2017 Try require_once(dirname(__FILE__).'/phpseclib/Crypt/RSA.php); Untested and I'm in a bit of a rush to get gone, but it should work for you. Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554301 Share on other sites More sharing options...
CloudWindMoonSun Posted November 30, 2017 Author Share Posted November 30, 2017 (edited) In regards to "white page" Create a page - ie my_error_find.php and include the file you are testing to see if you can jump a useful error out. <?php ######### ERROR FINDING PAGE ######### ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('error_reporting', E_ALL); error_reporting(E_ALL); error_reporting(-1); include("phpseclib/Crypt/RSA.php"); // <<== PATH TO FILE WITH ERRORS include("foo.php"); // <<== PATH TO FILE WITH ERRORS ?> Thanks BigB, using your code, I got some information about the problem. Here they are: Warning: include_once(Math/BigInteger.php): failed to open stream: No such file or directory in /Users/hhhh/Sites/MyFolder/phpseclib/Crypt/RSA.php on line 496 Warning: include_once(): Failed opening 'Math/BigInteger.php' for inclusion (include_path='.:') in /Users/hhhh/Sites/MyFolder/phpseclib/Crypt/RSA.php on line 496 Fatal error: Class 'Math_BigInteger' not found in /Users/hhhh/Sites/MyFolder/phpseclib/Crypt/RSA.php on line 554 The fie BigInteger.php is actually located inside /users/hhhh/Sites/MyFolder/phpseclib/Math/ and it's used from within RSA.php. MyFolder (folder) | |--- phpseclib (folder) | | | |--- Crypt (folder) | | | | | |--- RSA.php | | | |--- Math (folder) | | | |--- BigInteger.php | |--- foo.php The thing is as you know phpseclib library is not mine and I hesitate to change it unless if there is no other way to do this. It seems that phpseclib cannot find the classes using relative path. Edited November 30, 2017 by CloudWindMoonSun Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554304 Share on other sites More sharing options...
ginerjm Posted December 1, 2017 Share Posted December 1, 2017 Why not use a php var to set a path that gets you into your MyFolder directory? And then use a reference that looks like: $root_folder = '/xxx/xxx/MyFolder'; $root_folder . '/phpseclib....'; and $root_folder . '/phpsceclib/Math.....'; and $root_folder . '/foo.php....'; Assuming that you will be keeping the same layout as you have described, this allows for the whole thing to be migrated perhaps in the future to a new parent folder. Don't know why the references to namespaces came up. I don't see the need and since I have never used one don't understand how they could help. What you are experiencing is simply a reference problem in my mind. PS - personally I think it is a bad idea to use a folder name having a dot in it. Old school DOS guy here who would have had FITS with that kind of code. Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554337 Share on other sites More sharing options...
Solution taquitosensei Posted December 1, 2017 Solution Share Posted December 1, 2017 You could set the include path. $path = '/path/to/MyFolder'; set_include_path(get_include_path().PATH_SEPARATOR.$path); include("phpseclib/Crypt/RSA.php"); Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554339 Share on other sites More sharing options...
ginerjm Posted December 1, 2017 Share Posted December 1, 2017 One way or the other..... Personally I like the more obvious approach. Using the include path is a good method but I think that it s/b reserved for more general application usage. This reference is pertinent to a particular usage and (imo) s/b more apparent to the next person to come along and try to read this script. Place the root path in a global var or add it to a module that is a standard include in the application scripts Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554345 Share on other sites More sharing options...
CloudWindMoonSun Posted December 1, 2017 Author Share Posted December 1, 2017 (edited) You could set the include path. $path = '/path/to/MyFolder'; set_include_path(get_include_path().PATH_SEPARATOR.$path); include("phpseclib/Crypt/RSA.php"); I changed my code as following: $path = '/Users/hhhh/Sites/MyFolder'; set_include_path(get_include_path() . PATH_SEPARATOR . $path); include("phpseclib/Crypt/RSA.php"); But the only thing that changed is a slight change in the error I get: Warning: include_once(Math/BigInteger.php): failed to open stream: No such file or directory in /Users/hhhh/Sites/MyFolder/phpseclib/Crypt/RSA.php on line 496 Warning: include_once(): Failed opening 'Math/BigInteger.php' for inclusion (include_path='.::/Users/hhhh/Sites/MyFolder') in /Users/hhhh/Sites/MyFolder/phpseclib/Crypt/RSA.php on line 496 Fatal error: Class 'Math_BigInteger' not found in /Users/hhhh/Sites/MyFolder/phpseclib/Crypt/RSA.php on line 554 Why not use a php var to set a path that gets you into your MyFolder directory? And then use a reference that looks like: $root_folder = '/xxx/xxx/MyFolder'; $root_folder . '/phpseclib....'; and $root_folder . '/phpsceclib/Math.....'; and $root_folder . '/foo.php....'; Assuming that you will be keeping the same layout as you have described, this allows for the whole thing to be migrated perhaps in the future to a new parent folder. Don't know why the references to namespaces came up. I don't see the need and since I have never used one don't understand how they could help. What you are experiencing is simply a reference problem in my mind. PS - personally I think it is a bad idea to use a folder name having a dot in it. Old school DOS guy here who would have had FITS with that kind of code. I tried your suggestion and change the code as following: $root_folder = '/Users/hhhh/Sites/MyFolder'; $root_folder . '/phpseclib'; $root_folder . '/phpseclib/Crypt'; $root_folder . '/phpsceclib/Math'; $root_folder . '/foo.php'; Now I get this error: Fatal error: Class 'Crypt_RSA' not found in /Users/hhhh/Sites/MyFolder/foo.php on line 46 p.s. BTW, there is no dot in any of my folder names! Edited December 1, 2017 by CloudWindMoonSun Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554362 Share on other sites More sharing options...
ginerjm Posted December 1, 2017 Share Posted December 1, 2017 From your original post: my bad I guess - foo.php resembled a folder to me at first glance. AS for your attempt to include something from the Math folder - look at what you are doing. Envision what the real path looks like that you are asking php to process. You're missing something. And as for last box of code: What are you doing there? Trying to build one long string of folder names? What I gave you was an EXAMPLE of how my suggestion could be used in your example - not a suggestion to join all the folder names into one variable - which my example is not doing. PS - if you already define a $path using / as separators, why do you need to use the constant right after that for another definition? Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554363 Share on other sites More sharing options...
CloudWindMoonSun Posted December 2, 2017 Author Share Posted December 2, 2017 (edited) You could set the include path. $path = '/path/to/MyFolder'; set_include_path(get_include_path().PATH_SEPARATOR.$path); include("phpseclib/Crypt/RSA.php"); @taquitosensei, thank you very much. I changed my code as following and it now works like a charm. $path = '/Users/hhhh/Sites/MyFolder/phpseclib'; set_include_path(get_include_path() . PATH_SEPARATOR . $path); include("phpseclib/Crypt/RSA.php");tax Edited December 2, 2017 by CloudWindMoonSun Quote Link to comment https://forums.phpfreaks.com/topic/305796-how-to-include-files-in-other-folders/#findComment-1554364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.