Solved

PHP include tools from a central folder

Posted on 2013-02-06
11
294 Views
Last Modified: 2013-02-10
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
Comment
Question by:Glen Gibb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38861692
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
 

Author Comment

by:Glen Gibb
ID: 38861909
That is, I can include the absolute path, for example:  "/var/tools/tools.php"?

Capt
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38862135
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!

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38862149
Look at the examples for fread().  http://www.php.net/manual/en/function.fread.php
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 38864074
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
 

Author Comment

by:Glen Gibb
ID: 38864464
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
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 38864816
You might want to copy the library into each website folder.
0
 

Author Comment

by:Glen Gibb
ID: 38864839
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38871398
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
 

Author Closing Comment

by:Glen Gibb
ID: 38873597
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38873710
Excellent, and thanks for the points, ~Ray
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question