Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts

    Creating a PHP Library - Properly linking folders

    I've got a project I'm working on which requires some code that is going to end up being very reusable. In order to get the most value out of my code, I want to store it 100% separate from my project.

    The library itself is basically two classes + some images. How can I set things up so that in any other project, I can do something like

    require '../libs/mylib/mylib.php'

    and everything will fall into place? This also includes using those images. Right now, in the library, I simply say "images/image.png". The problem: That path is relative to the file that included the library, not the library. I want to store the images as part of the library.

  • #2
    Regular Coder
    Join Date
    Dec 2008
    Posts
    133
    Thanks
    15
    Thanked 0 Times in 0 Posts
    use require_once(""); or include_once(""); ?

    I guess you could use absolute links like "http://example.net/libs/mylib/mylib.php"
    But what you've said
    require_once ("../libs/mylib/mylib.php")
    if I've understood your question, i can't see you having a problem?

  • #3
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    That would work if I only wanted to use the library in one place or on one server. I'm having trouble because I want to be able to use this code anywhere on any different server without having to change much more than a configuration path or something.

    Note that the include of mylib.php can change. However, mylib.php includes several other files that are part of the library. Because those paths are relative to the file that included mylib.php, it will often be wrong. For example, if I put a demo.php file in the mylib directory and then also try to use the library from a different folder, it won't work in both cases.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •