0

Although this has been asked a thousand times I still managed to screw things up and not find an answer that serves my issue..

I have 2 files

C:\xampp\htdocs\MF\Pages\ads.php
C:\xampp\htdocs\MF\Pages\ads_view.php

I wanna include ads_view from ads what I do is

echo __DIR__;
var_dump(file_exists('ads_view.php'));

and what I get is

C:\xampp\htdocs\MF\Pages
bool(false)

Why can't I include this file when __DIR__ clearly states i'm in the parent folder? ads.php is also included from another file, if that makes any difference?

2 Answers 2

1

Please put the full path it should work

var_dump(file_exists('C:\xampp\htdocs\MF\Pages\ads_view.php'));

see documentation for file_exists

Sign up to request clarification or add additional context in comments.

Comments

1

You can also use the DOCUMENT_ROOT variable in $_SERVER for portability.

var_dump(file_exists($_SERVER['DOCUMENT_ROOT'] . '\MF\Pages\ads_view.php'));

and

include $_SERVER['DOCUMENT_ROOT'] . '\MF\Pages\ads_view.php';

4 Comments

It's actually weird because include works just well with relative paths but file_exists doesn't >.<
Thats because, like meda said, fil_exists expects a complete path to a file while include also works with current directory and relative paths (like ../../ etc.). But i like to use document_root so i can copy the code to other files in other directories without changing the path. A "c:\path..." etc. would work too but if i copy the code to another site it would stop working.
you can simply use __DIR__ instead of document root
@php_nub_qq: You are right for just file_exists. Then its simpler to use __DIR__. But for an include, especially outside your own directory i would still use document_root and not a hard-coded path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.