0

I'm not understanding this. It's screwing up whole site because I'm using a php template.

Supposedly beginning a link with '/' starts me at the root according to every article I've read. However, when I begin with '/' it doesn't work at all.

My variables are located in

public_html/cis130/textfiles/php/variables.php

My template.php file is in

public_html/cis130/textfiles/php/template.php

My index.php file is located at

public_html/cis130/index.php

In my index.php file it reads,

<?php  
  require('/cis130/textfiles/php/variables.php');  
  $currentpage = $page[0];  
  require('/cis130/textfiles/php/template.php');  
?>

It doesn't work... I get "Fatal error: require(): Failed opening required '/cis130/final/textfiles/php/variables.php' (include_path='.:/usr/local/php54/pear') in /home/philumpt/public_html/cis130/final/index.php on line 3"

If I use relative links,

textfiles/php/file.php

then it works, but only for index.php. My other pages are located in 'public_html/cis130/textfiles/pages/'. If that's weird, it's what the teacher in my class is making us do.

  • Remember that PHP is a scripting / server side language - Unless you are using a constant var (such as $_SERVER['DOCUMENT_ROOT']) it won't know the path to the file you are looking for. '/' refers to the root of the file system. – AutomationNation May 16 '14 at 00:31
  • $_SERVER['DOCUMENT_ROOT'] doesn't work though. In the error it gives me the correct link to the file and then says it doesn't exist.. :) Weird. – Philumptuous May 16 '14 at 00:35
  • Does the relative link work? – AutomationNation May 16 '14 at 00:36
  • That's the only thing that works, but then when I'm in the pages directory I get errors because all the relative links are relative to index.php – Philumptuous May 16 '14 at 00:41

5 Answers5

0

Try this:

require($_SERVER['DOCUMENT_ROOT'].'/cis130/textfiles/php/variables.php');

$_SERVER['DOCUMENT_ROOT'] is how you specify the root directory in php.

ScorNinja
  • 69
  • 5
  • No, that didn't work. And it looks like it went further back than public_html – Philumptuous May 16 '14 at 00:17
  • My bad. I changed it to include the public_html/. – ScorNinja May 16 '14 at 00:21
  • Try one more time. I added the home/philumpt directories to it. – ScorNinja May 16 '14 at 00:33
  • No, it's saying "/home/philumpt/public_html/home/philumpt/public_html" so I guess you don't need all that. I just tried the absolute url with my domain and that didn't even work... What does this mean? "(include_path='.:/usr/local/php54/pear')" – Philumptuous May 16 '14 at 00:39
  • If you can figure out which directory `$_SERVER['DOCUMENT_ROOT']` is pointing to, you can just add the path onto it... – ScorNinja May 16 '14 at 00:43
  • Agreed with @ScorNinja - echo out the contents of that variable, and then find the filesystem location of your script file. – AutomationNation May 16 '14 at 00:45
  • echo "$_SERVER['DOCUMENT_ROOT']"; is /home/philumpt/public_html Doesn't make any sense. :) Maybe I'll just stick with html for now – Philumptuous May 16 '14 at 00:46
  • That means the root directory is public_html. So my original answer should work. – ScorNinja May 16 '14 at 00:52
  • I figured out a problem with this now when I made a link variable to equal '$_SERVER['DOCUMENT_ROOT'] . "/cis130/final/textfiles/php/variables.php";'. The link was to 'http://philumptuous.com/home/philumpt/public_html/cis130/final/textfiles/php/variables.php.' So it just basically doubled the root directory. – Philumptuous May 16 '14 at 00:52
  • The problem with `$_SERVER['DOCUMENT_ROOT']` is that it is inconsistent between platforms & installs. So it’s just unreliable when you least need that kind of headache. You’re much better off using `dirname(__FILE__)` if you need to automatically determine path. But the best way is to manually set a base path with a variable. – Giacomo1968 May 16 '14 at 03:25
0

Why don't you start from where index.php is? Like this:

<?php  
  require('textfiles/php/variables.php');  
  $currentpage = $page[0];  
  require('textfiles/php/template.php');  
?>

And for other pages use:

<?php  
  require('../php/variables.php');  
  require('../php/template.php');  
?>
CIRCLE
  • 4,501
  • 5
  • 37
  • 56
  • Because I have a whole bunch of other files being called within template.php that will be used for the other pages which are in a different directory than index.php. So that'll work but in my author page in 'textfiles/pages/authorpage.php' there are errors again because it's looking for textfiles in my pages directory. – Philumptuous May 16 '14 at 01:02
  • template.php requires header.php in 'textfiles/php/header.php' so if I'm in 'textfiles/pages/authorpage.php' there's an issue – Philumptuous May 16 '14 at 01:11
0

I think the easiest way to fix this would be to set your include directory and put all of your files in that.

In your php.ini file, find this line...

ini_set('include_path', '/usr/lib/pear');

...but exchange '/usr/lib/pear' with the path to the folder you would like to keep your documents in. It can be above or below the root directory.

Then, put all of your include files in there. No matter what level of the directory you are including from, it will look in that directory for them, making include files easy to write.

Or, you could set the folder path right there in the fuel you are updating. Make sure this is at the top of any php file that has an include. All of your require() functions will look in there first.

<?php
set_include_path('/path/to/folder');
StephenCollins
  • 785
  • 4
  • 10
0

Stop worrying about relative path by just using a base path set as a configuration so you are not constantly juggling relative locations. For example, in your main config file, you can define a base path like this:

$BASE_PATH = '/the/path/to/the/codebase/';

If you don’t know the base path to your files, then place this line at the top of your PHP code:

echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";

And load that page. Somewhere near the top should be a line that reads:

Your path is: /the/path/to/the/codebase/

Of course /the/path/to/the/codebase/ will be your actual file path, but that will be your base path. Then just set $BASE_PATH to that value.

Then when you do an require, the syntax would be:

require($BASE_PATH . '/cis130/textfiles/php/variables.php');

The benefit of this is no matter how deeply nested your codebase becomes, you will always be anchored to the value of $BASE_PATH. And your life will be made tons easier thanks to not having to worry about relative path issues.

I would also recommend using require_once instead of require to avoid scenarios where your script might inadvertently attempt to load the same file more than once.

require_once($BASE_PATH . '/cis130/textfiles/php/variables.php');
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • Yeah, I'm not too well versed in PHP to understand what you're saying. I can't access php.ini on my school's server, and I'm just using my own while I develop it. So I don't know how to set $BASE_PATH. If I sound stupid, that's because I am stupid. – Philumptuous May 16 '14 at 01:14
  • @Philumptuous “ I can't access php.ini on my school's server” No, this has nothing to do with `php.ini` at all. You are missing the point. This is 100% in your control right now. What I am saying is you should set a variable in your code called `$BASE_PATH` that would be the actual base path to your code. That way you don’t have to play games with relative paths. I assure you your teacher will be impressed. – Giacomo1968 May 16 '14 at 01:21
  • I'm confused about what to make the path. How do I write it? The only paths working are relative ones – Philumptuous May 16 '14 at 01:32
  • @Philumptuous Look at my edit just now on how to figure out your real base path. Which is the real path on the file system where the PHP script is running. Past that, not much else I can say. This is a very fundamental thing any PHP programmer should be able to do. – Giacomo1968 May 16 '14 at 02:03
  • 1
    That works! Thanks.. I was confused. I thought you were just giving me another way to write $_SERVER['DOCUMENT_ROOT'] which kept doubling the root in the path for whatever reason. – Philumptuous May 16 '14 at 03:18
0

In linux, URIs starting with '/' are full paths. Example, '/run/shm/', because / is like saying C: in Windows.

Writing 'dir/something.php' should work, if your PHP file is in the parent dir of dir.

Also, you can use __DIR__ to refer to the current directory.

Example: $file = __DIR__.'filename.php'

http://www.php.net/manual/en/language.constants.predefined.php

URLs starting with '/' are relatives to the site's root but only in client-side, html. Example, you can link something: <a href="/contact">Contact us</a>

JorgeeFG
  • 5,651
  • 12
  • 59
  • 92