bugzy Posted July 9, 2012 Share Posted July 9, 2012 Hello guys I'm just a beginner and I have been idle on programming for almost a year already and I find myself confuse again. I'm getting this error Warning: require_once(back/includes/connection.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\back\admin\index.php on line 1 Fatal error: require_once() [function.require]: Failed opening required 'back/includes/connection.php' (include_path='.;C:\php5\pear') in C:\wamp\www\back\admin\index.php on line 1 I know it's a path problem and I almost tried everything and I still can't figure it out. So basically I have folder "admin" and a file "index" where I'm calling for the file "connection.php" which is on the folder "includes" on the root folder... Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/ Share on other sites More sharing options...
xyph Posted July 9, 2012 Share Posted July 9, 2012 '../includes/connection.php' $_SERVER['DOCUMENT_ROOT'] . 'includes/connection.php' Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360330 Share on other sites More sharing options...
Mahngiel Posted July 9, 2012 Share Posted July 9, 2012 What is the full path to the page you're loading and the ful path to the file you're trying to include? You can include pages one of two ways, relatively or full path. Relatively: the location from the current file. This is the most common and when you see things like ../assets/ Full Path: the path from root / c: Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360333 Share on other sites More sharing options...
bugzy Posted July 9, 2012 Author Share Posted July 9, 2012 '../includes/connection.php' $_SERVER['DOCUMENT_ROOT'] . 'includes/connection.php' Thanks sir! Now I know! Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360337 Share on other sites More sharing options...
bugzy Posted July 9, 2012 Author Share Posted July 9, 2012 Sorry guys need to unsolved it because my problem now is related to to this.. I'm using a single CSS only which is on the stylesheet folder. So when my index.css is on the root I'm calling it like this <link rel="stylesheet" type="text/css" href="stylesheet/public.css" media="screen" /> problem is when I'm calling that CSS file on another folder, it can't find it because I'm using the same header as my files in the root directory.. As much as possible I want to use one CSS only. I don't want to use any php yet because the problem might only be on the path only.. Anyone? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360357 Share on other sites More sharing options...
xyph Posted July 9, 2012 Share Posted July 9, 2012 Well, its kinda hacky, but you can define the absolute path to your CSS file, and then use the absolute path of the current file to make a relative link. Here's a snippet from SO, in use. http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php <?php $css = '/path/to/webroot/css/main.css'; $current = '/path/to/webroot/html/thisfile.html'; // you could use $_SERVER['SCRIPT_FILENAME'] for $current echo getRelativePath($current, $css); // outputs ../css/main.css function getRelativePath($from, $to) { $from = explode('/', $from); $to = explode('/', $to); $relPath = $to; foreach($from as $depth => $dir) { // find first non-matching dir if($dir === $to[$depth]) { // ignore this directory array_shift($relPath); } else { // get number of remaining dirs to $from $remaining = count($from) - $depth; if($remaining > 1) { // add traversals up to first matching dir $padLength = (count($relPath) + $remaining - 1) * -1; $relPath = array_pad($relPath, $padLength, '..'); break; } else { $relPath[0] = './' . $relPath[0]; } } } return implode('/', $relPath); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360365 Share on other sites More sharing options...
bugzy Posted July 9, 2012 Author Share Posted July 9, 2012 Well, its kinda hacky, but you can define the absolute path to your CSS file, and then use the absolute path of the current file to make a relative link. Here's a snippet from SO, in use. http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php <?php $css = '/path/to/webroot/css/main.css'; $current = '/path/to/webroot/html/thisfile.html'; // you could use $_SERVER['SCRIPT_FILENAME'] for $current echo getRelativePath($current, $css); // outputs ../css/main.css function getRelativePath($from, $to) { $from = explode('/', $from); $to = explode('/', $to); $relPath = $to; foreach($from as $depth => $dir) { // find first non-matching dir if($dir === $to[$depth]) { // ignore this directory array_shift($relPath); } else { // get number of remaining dirs to $from $remaining = count($from) - $depth; if($remaining > 1) { // add traversals up to first matching dir $padLength = (count($relPath) + $remaining - 1) * -1; $relPath = array_pad($relPath, $padLength, '..'); break; } else { $relPath[0] = './' . $relPath[0]; } } } return implode('/', $relPath); } ?> Thanks. I'll definitely try this. Btw is this the standard? How are you guys doing it? Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360367 Share on other sites More sharing options...
xyph Posted July 9, 2012 Share Posted July 9, 2012 Personally, I just keep any files that need to output HTML in the same directory. If I need the appearance of folders for a pretty URL, I use mod_rewrite. Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360369 Share on other sites More sharing options...
jcbones Posted July 9, 2012 Share Posted July 9, 2012 I always use the full URL for any pictures, CSS, JS, etc. Then I don't have to worry about a path issue. Quote Link to comment https://forums.phpfreaks.com/topic/265440-php-error-failed-to-open-stream/#findComment-1360386 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.