soycharliente Posted June 29, 2007 Share Posted June 29, 2007 I have a file in my root directory called: charts.php I have this line of code in my index.php (which is in another directory two levels deep): require("/charts.php"); I am getting this error: Warning: main(/charts.php): failed to open stream: No such file or directory in /supersecretpath/index.php on line 2 Warning: main(/charts.php): failed to open stream: No such file or directory in /supersecretpath/index.php on line 2 Fatal error: main(): Failed opening required '/charts.php' (include_path='.:/usr/local/lib/php') in /supersecretpath/index.php on line 2 Can someone tell me what is wrong? Quote Link to comment Share on other sites More sharing options...
Caesar Posted June 29, 2007 Share Posted June 29, 2007 Which is two directories deep? index.php? or charts.php? If index.php is two levels lower... <?php require'../../charts.php'; ?> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Author Share Posted June 29, 2007 Yeah. I thought about doing that. But I didn't want to have to worry about remembering to update the path if I move the file. I wanted to just go from the root. ROOT: charts.php /folder1/folder2/: index.php <-- with require("/charts.php"); Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 require("charts.php");//for same dir require("../charts.php");//outside the folder require("foldername/charts.php");//inside another folder hope that helps Quote Link to comment Share on other sites More sharing options...
Caesar Posted June 29, 2007 Share Posted June 29, 2007 You're using a relative path. The path is always going to change, if you move the file. Unless... <?php $path = $_SERVER['HTTP_HOST'].'/'; require $path.'charts.php'; ?> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Author Share Posted June 29, 2007 You're using a relative path. The path is always going to change, if you move the file. Unless... <?php $path = $_SERVER['HTTP_HOST'].'/'; require $path.'charts.php'; ?> How is that different from just using / to go to the root folder and then type the filename? I _WAS_ fairly sure that / went to the root, but now I'm doubting my knowledge. How is starting off with / a relative path? I've always been told that's an absolute path to the root. Quote Link to comment Share on other sites More sharing options...
Caesar Posted June 29, 2007 Share Posted June 29, 2007 An absolute path is defining an explicit path... <?php require 'http://mysite.com/index.php' ?> Anytime you don't define a path to the server, the folder paths in your HTML/PHP are going to be seen "relative" to the location you are currently at. If my script is in one folder below the root/public directory... <?php require '../charts.php'; ?> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Author Share Posted June 29, 2007 So, to you, what does / do? Quote Link to comment Share on other sites More sharing options...
Caesar Posted June 29, 2007 Share Posted June 29, 2007 If I'm on my Linux box...then yes, I would type: root@myserver[~]# cd / And that would take me to the root. Or.. root@myserver[~]# cd /tmp To move into the /tmp folder, one level lower than root. Quote Link to comment Share on other sites More sharing options...
Caesar Posted June 29, 2007 Share Posted June 29, 2007 Check this for more info... Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Author Share Posted June 29, 2007 So can anyone tell me why require("/charts.php"); doesn't work? Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 So can anyone tell me why require("/charts.php"); doesn't work? / means that the file after/ is under folder before/ so when you have something like this require("/charts.php") it like saying that harts.php is inside the blank folder which is invalid Quote Link to comment Share on other sites More sharing options...
rcorlew Posted June 29, 2007 Share Posted June 29, 2007 your code would be <?php require("../../charts.php"); ?> ../ will move you down one level towards the root Quote Link to comment Share on other sites More sharing options...
corbin Posted June 29, 2007 Share Posted June 29, 2007 teng84 that is entirely wrong. Go to cmd or terminal and type "cd /" and it will take you to the top of what ever drive your under. ;p Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 teng84 that is entirely wrong. Go to cmd or terminal and type "cd /" and it will take you to the top of what ever drive your under. ;p ^^^ what is there a difference with what i said when you type cd/ you will get the content under that right and cd.. back and whats your point ?? Quote Link to comment Share on other sites More sharing options...
corbin Posted June 29, 2007 Share Posted June 29, 2007 I'm saying if you're at c:\program files\some\program and you change to / it will take you to c:\.... You're saying it's invalid and won't go to any folder if I read your post correctly. Sorry if I misunderstood, or if I came across the wrong way.... Was just tryin to clear things up ;p Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 tell me is this correct? include '/file.php';// for me thats wrong and it wont work if you say its wrong then nothing to argue you dont understand my xplanation should be include '../file.php' or include 'file.php' or include 'foldername/file.php' Quote Link to comment Share on other sites More sharing options...
shoz Posted June 29, 2007 Share Posted June 29, 2007 You're using a relative path. The path is always going to change, if you move the file. Unless... <?php $path = $_SERVER['HTTP_HOST'].'/'; require $path.'charts.php'; ?> How is that different from just using / to go to the root folder and then type the filename? I _WAS_ fairly sure that / went to the root, but now I'm doubting my knowledge. How is starting off with / a relative path? I've always been told that's an absolute path to the root. / can be thought of as the top directory (root) of a linux system. All paths start from "/". Apache has what's called the DOCUMENT ROOT. It's the top directory for files that apache will serve. In your case that directory could be "/home/charlieholder/www". require("/filename") looks for "filename" starting at "/" and not "/home/charlieholder/www", while a request over the web for "http://www.host.com/filename" causes apache to look for the file in "/home/charlieholder/www/". You can use the variable $_SERVER['DOCUMENT_ROOT'] to get the root you're looking for. It will contain (in this example) the value "/home/charlieholder/www" Btw, keep in mind that a require("http://www.host.com/filename") also requests the page over the web. Compared to require("/home/charlieholder/www/filename") which uses the filesystem path and is most likely what you'd like to do because a request over the web will make apache parse the file as a php script and the OUTPUT will be included in your script rather than the code itself. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 29, 2007 Share Posted June 29, 2007 tell me is this correct? include '/file.php';// for me thats wrong and it wont work if you say its wrong then nothing to argue you dont understand my xplanation should be include '../file.php' or include 'file.php' or include 'foldername/file.php' As shoz explained, / goes to the root of the drive (or under linux the root of the mounting), meaning that if you had everything running on c:\apache foundation\apache2\htdocs\ and you were to include '/file.php', php would look for c:/file.php. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 corbin your on the wrong track i guess include '/file.php'; try to run something like that and tell if it will work then tell me waht i mean note / slash place that on the include just like this include '/file.php'; Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Author Share Posted June 29, 2007 <?php $path = $_SERVER['HTTP_HOST'].'/'; require $path.'charts.php'; ?> So what Caesar said earlier is best? Or use: <?php $path = $_SERVER['DOCUMENT_ROOT'].'/'; require $path.'charts.php'; ?> EDIT: Sounds like they are both valid, but the second puts less stress on server? Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 no what if the location is folder1/folder1/folder1/ this====will only $path = $_SERVER['HTTP_HOST'].'/'; require $path.'charts.php'; ^^ rootdir /charts.php remember we still have two folder folder1/folder1/ now think is that the best? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Author Share Posted June 29, 2007 charts.php IS in the root dir. The file that is trying to include it is the one that is in a different directory. Quote Link to comment Share on other sites More sharing options...
shoz Posted June 29, 2007 Share Posted June 29, 2007 <?php $path = $_SERVER['HTTP_HOST'].'/'; require $path.'charts.php'; ?> So what Caesar said earlier is best? Or use: <?php $path = $_SERVER['DOCUMENT_ROOT'].'/'; require $path.'charts.php'; ?> EDIT: Sounds like they are both valid, but the second puts less stress on server? If you're using an absolute path use $_SERVER['DOCUMENT_ROOT']. It's more than just the stress on the server. For example page1.php <?php require($_SERVER['DOCUMENT_ROOT'].'/myvars.php'); echo $var1 ?> myvars.php $var1 = 'hello world'; As it stands now, the output of the page1.php script will be 'hello world' but if the require line is changed to "require($_SERVER['HTTP_HOST'].'/myvars.php');" apache will parse myvars.php and the OUTPUT will be included in page1.php which in this case is nothing and page1.php will not have a $var1 variable set meaning there will be no output other than possibly errors referring to the fact that the $var1 variable does not exist. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 29, 2007 Share Posted June 29, 2007 corbin your on the wrong track i guess include '/file.php'; try to run something like that and tell if it will work then tell me waht i mean note / slash place that on the include just like this include '/file.php'; That would work perfectly fine assuming the file existed at the top of the drive or the document root... Forgot which one php maps it to... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.