Solved

PHP path reference for ../

Posted on 2013-01-12
13
277 Views
Last Modified: 2013-01-13
What determines how PHP evaluates things like

                               include( ../file.html);

My experience is that this is normally evaluated with respect to the location of the script that contains the line but I've come across a situation where it's being evaluated with respect to an absolute address.  How do you tell PHP to evaluate these with respect to some absolute address?

Thanks for any info.

Steve
0
Comment
Question by:steva
  • 6
  • 6
13 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 38769840
'../' means the directory above the current directory.  However, when you 'include', the reference is the location of the main file.  '/' (just a slash) is the root of your web directory.  So "/images/blank.gif" means to find "blank.gif" in the 'images' directory which is found in your web root.

Note that if you use include ("http://yourwebsite.com/file.php"), you will get the results of that PHP file, not it's contents or code.
0
 
LVL 20

Assisted Solution

by:Mark Brady
Mark Brady earned 250 total points
ID: 38769915
You can set the include path either in the php.ini file or directly from your script. Here's a quick example of setting it inside your script.

$path = '/usr/lib/pear';

set_include_path(get_include_path() . PATH_SEPARATOR . $path);

I usually create a config file which sets all that stuff and things like constants etc... then include that file with all others. As Dave stated above, it is relative to your document.
For security reasons "include" or "require" statements only work with local files (on the same server as your main script).
0
 

Author Comment

by:steva
ID: 38770497
Thanks.  

I think my issues are with the CodeIgniter framework I'm working in.  I'm trying to make sense of this from a PHP view but CodeIgniter seems to be doing something above PHP on its own.  

A file has

require('../config.php');

and if I insert code in that file to display the results of get_include_path() I see:

                           include path=.:/usr/share/php:/usr/share/pear

But config.php is actually being pulled relative to the top codeigniter directory, i.e., up one from ci and down to the config.php there.

Thanks for your input, though.

Steve
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38770613
That's from 'php.ini'.  'include path' is where PHP looks if the file is Not found in the specified place.
http://php.net/manual/en/function.include.php
0
 

Author Comment

by:steva
ID: 38770999
But in my case PHP is finding a way to resolve the ../ without using the current directory or what's in the include_path. The full directory involved is
path
When the include(../config.php) in uploader_settings.php is evaluated, the circled config.php comes in.  Somehow CodeIgniter has inserted its own path so that the ci folder - the top of the CodeIgniter stuff -  is the reference for evaluating ../.

This will probably become clearer after studying more about CodeIgniter.  I'm just surprised that it can do anything that overrides the basic PHP path evaluation.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38771222
"PHP path evaluation"??  PHP uses the path structures, directories and files, of the underlying operating system.

In addition, " include path=.:/usr/share/php:/usr/share/pear" are places that you can't put any files normally (unless you get 'root' access on the server).

If you want the 'config.php' in the same directory, that what you put:  include(config.php);
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:steva
ID: 38771364
Yeah.  I don't know.  Basically I don't understand how the include(../config.php) in uploader_settings.php above loaded the config.php circled in the figure above.  I need to go deeper into CodeIgniter to understand what going on..

Cheers
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38772395
Are you seeing this on your own computer or on your web hosting?
0
 

Author Comment

by:steva
ID: 38772680
Web hosting,  on a Linux/Apache server.  But it is doing what it should.  It needs to load that particular config file that I've circled.  That's where all the config options are for the upload operation it's doing.  I just don't know how include(../config.php) from uploader_settings.php accomplishes that.  

Have you done anything with CodeIgniter, or other PHP frameworks?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38772707
I generally avoid frameworks like the plague.  It's difficult enough without some program pretending it knows better than you.  While I'm sure they are useful for the things they are intended for, I have no idea what that might be.

I especially would not run any IDEs that provide their own 'web server' because your actual web hosting is always going to be somewhat different.  I have at least 3 versions of Apache and 3 versions of IIS that I have dealt with.  Add to that 3 or 4 versions of PHP and MySQL and MS SQL,  I can't have my development situation limited to what only one IDE can provide.
0
 

Author Comment

by:steva
ID: 38772749
I generally avoid frameworks like the plague.  It's difficult enough without some program pretending it knows better than you.  While I'm sure they are useful for the things they are intended for, I have no idea what that might be.
That was pretty much my opinion, until some code was dropped in my lap that I had to understand and it used CodeIgniter, so I was dragged kicking and screaming to it.

But it's not bad! Basically you get "professional" code that does the things that are typically wily in PHP (database, security, login, sessions, file uploads, image manipulation . . .)  and your finished pages aren't a godawful  mixture of PHP, HTML and database code.   If you have 32 minutes you might find this video - "CodeIgniter from Scratch" -  interesting: http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-1/
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38772853
But I almost specialize in fixing "godawful mixtures of PHP, HTML and database code".  That's where I make my money on the web.  I have 'undone' Dreamweaver, Adobe Go Live, Microsoft Publisher and Front Page, and Apple's iWeb to make them work.  There are several that I won't touch such as Smarty templates, Xoops, and NetObjects Fusion.  While they may be well organized, they are complicated beasts and not worth my effort to understand and fix.
0
 

Author Comment

by:steva
ID: 38772987
OK, then it's probably not for you.   I make my money on the web building things for people, and to me it looks like it might replace the hammer in my toolbox with a nail gun - so I'm interested.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now