Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

PHP include tools from a central folder

Experts,

I find myself coding the same things repeatedly.

I'd like to put some oft-used code in a "Tools" folder to be re-used whenever I do a new website.   (I'm lousy at the relative/absolute path concept -- no surprise).

How do I include files from outside the DocumentRoot?

Capt
0
Glen Gibb
Asked:
Glen Gibb
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
First, you do have to have permission to access the files.  Second, you usually have to use the full path on the server.  If you are on shared hosting, you probably won't be allowed to do it anyway.
0
 
Glen GibbAuthor Commented:
That is, I can include the absolute path, for example:  "/var/tools/tools.php"?

Capt
0
 
Dave BaldwinFixer of ProblemsCommented:
I think that will depend on you having permission to access that file and directory.  I thought I had an example but I can't find it at the moment.  Try it, that's what I usually do.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Dave BaldwinFixer of ProblemsCommented:
Look at the examples for fread().  http://www.php.net/manual/en/function.fread.php
0
 
Mohamed AbowardaSoftware EngineerCommented:
You want to create a PHP library? what you can do is that you can create the library in a folder and copy this folder into your website /www/ and use PHP require() and require_once() to use the library in your script.
0
 
Glen GibbAuthor Commented:
Thanks, Experts!

Sometimes we just don't have the language.  Creating a library is exactly the right terminology.

However, I have more than one website residing on the same server.  So if I go back to the Document root, "www.example.com/", it doesn't include the DocRoot for "www.other.com/".

Thus, how to reference a folder "outside the DocRoot"?  That is, how do I include the library for both?  (BTW, I have rwx permissions.)

Capt
0
 
Mohamed AbowardaSoftware EngineerCommented:
You might want to copy the library into each website folder.
0
 
Glen GibbAuthor Commented:
Ouch!  I'm copying them now.  

The original question is, can I put them in a central library outside the DocRoot and include them in my websites?

I'm looking in Webmin.  Does the "per-directory option" allow me to use a central library?

If so, how?

Capt
0
 
Ray PaseurCommented:
Who is your hosting company?  On my shared server, I have access to the files above the document root (in other words, my "account" has several web sites, each with its own public_html web root directory).  If your hosting company cannot hook this up for you, change hosts ASAP.  Let me know if you want a recommendation.

Once you've got the common elements in the "account" directory instead of the web root, you can include() the shared library scripts with notation something like this...

require_once('../shared/common.php');

Best regards, ~Ray
0
 
Glen GibbAuthor Commented:
I'm hosting this on my own Linux server at home.   But I see how to move up a level thanks to your example.

Capt
0
 
Ray PaseurCommented:
Excellent, and thanks for the points, ~Ray
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now