سؤال

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.

هل كانت مفيدة؟

المحلول

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');

نصائح أخرى

Try this:

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

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

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');  
?>

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');

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>

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top