Jump to content

[SOLVED] require() error


soycharliente

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ??

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.