TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-07-2010, 01:24 PM   #1 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Calculator includes and reading txt file

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

which also includes ../includes/head.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
Tim Dobson is offline  
Reply With Quote
Old 07-07-2010, 06:40 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tim Dobson (07-08-2010)
Old 07-08-2010, 10:43 PM   #3 (permalink)
The Contributor
 
Tim Dobson's Avatar
 
Join Date: Feb 2010
Posts: 69
Thanks: 16
Tim Dobson is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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($fhfilesize($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.
Tim Dobson is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
PHP/MySQL tips and tricks jaswinder_rana Tips & Tricks 6 07-17-2005 01:56 AM


All times are GMT. The time now is 07:39 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design