Jump to content

How to include files in other folders?


CloudWindMoonSun
Go to solution Solved by taquitosensei,

Recommended Posts

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");
            ...
        }
    }
?>

 

Link to comment
Share on other sites

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


?>
Link to comment
Share on other sites

 

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 by CloudWindMoonSun
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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 by CloudWindMoonSun
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 

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 by CloudWindMoonSun
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.