TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)

 Tim Dobson 07-07-2010 01:24 PM

Just a little thing im doing to get my experience up. Im trying many different things and i would like to know how to go about this little thing im trying

Iv got index.php in the main directory - uses include/head.php

iv got head.php which is in the includes directory - this reads a text file which is in a location going from main directory Admin/Docs/head.txt

There is nothing wrong with this it reads the file exactly how i want it.

However iv got now in the admin directory index.php

So the problem i have got is its not seeing it correctly because of the directory location when it reads the head.php the text file location is incorrect reading it as Admin/docs/head.txt and because it is already in the admin location from the admin/index.php it pulls out a file does not exist.

How would i go about fixing this.

Thanks ;-)

 Salathe 07-07-2010 06:40 PM

The best way to make sure the right path is used, is to provide an absolute one. Commonly that means using something like __DIR__ (or dirname(__FILE__) if you're not blessed with being able to use PHP 5.3 yet) to get the absolute path to the file the constant is written in.

For example, inside your head.php file:

PHP Code:

 include __DIR__ . '/../Admin/Docs/head.txt';  
If the path to head.php was /home/path/to/site/includes/head.php then __DIR__ would be /home/path/to/site/includes. That would make the included file be located at /home/path/to/site/includes/../Admin/Docs/head.txt which is just the same as /home/path/to/site/Admin/Docs/head.txt.

With that, you would continue including the head.php as you are and the path would always be correct for the head.txt file.

 Tim Dobson 07-08-2010 10:43 PM

Quote:
 Originally Posted by Salathe (Post 30800) The best way to make sure the right path is used, is to provide an absolute one. Commonly that means using something like __DIR__ (or dirname(__FILE__) if you're not blessed with being able to use PHP 5.3 yet) to get the absolute path to the file the constant is written in. For example, inside your head.php file: PHP Code:  include __DIR__ . '/../Admin/Docs/head.txt';   If the path to head.php was /home/path/to/site/includes/head.php then __DIR__ would be /home/path/to/site/includes. That would make the included file be located at /home/path/to/site/includes/../Admin/Docs/head.txt which is just the same as /home/path/to/site/Admin/Docs/head.txt. With that, you would continue including the head.php as you are and the path would always be correct for the head.txt file.
ok i think i am with you but i failed to get that to work so i thought i would try something like

PHP Code:

 <?php                      $myFile = "E:\domain\link\sb0t.tentun.co.uk\goin\to\the\file\head.txt";$fh = fopen($myFile, 'r');$theData = fread($fh, filesize($myFile)); fclose($fh); echo '' .$theData . ''; ?> 
Which correct me if i am wrong would do vertualy the same thing?

It is getting the absolute path and then printing out the data from the file. Atleast it seems to work for me doing it such a way.

 All times are GMT. The time now is 09:17 PM.