Log in

View Full Version : images not showing with require_once



vineet
11-30-2008, 01:34 PM
hi all

i m having problem with including files and images geting disappear.

i have images folder on the root.
i have header.php in my included_files folder on root.

if i include header.php in my files that are on the root with this code


<?php require_once("included_files/header.php");?>


then all the images of the header.php are shown perfectly alright.


But

if i include header.php in my files that are inside any folder on the root with this code


<?php require_once("../included_files/header.php");?>


then all the images of the header.php are not shown. they disappear.


Like i have folder2 on root. and there is file2 inside folder2. Then i used this code


<?php require_once("../included_files/header.php");?>


to include my header file. but with this code my images of header.php file disappear.


i m not able to understand as we use "../" if the file is inside in any folder on root.


thanks
vineet

JasonDFR
11-30-2008, 05:48 PM
I have had the same problem and have never really found a solution I was totally happy with.

I eventually decided to NOT use ../ to navigate in and out of directories when including files.

I think the following is the best solution:



<?php require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php"); ?>

And for html within the header that references your pictures, do this:


<img src="/images/picture.jpg" alt="" width="258px" height="149px" />

src="/" When you start with the forward slash, you can build the file reference from the public web root (www or public_html). For example, you can link to index.php (the "home page") by simply doing this:


<a href="/">Link to home page</a>

If anyone else has any ideas about this I would love to hear them too. This is something I struggled with before until I came up with the above solution. But again, I am not convinced it is the best solution.

Good Luck,

Jason

vineet
12-01-2008, 04:19 AM
hi jason

if i use


<?php require_once($_SERVER['DOCUMENT_ROOT'] . "/included_files/hdr.php"); ?>

then the header file get missed. i get mysql error

Warning: require_once(E:/xampp/htdocs/included_files/hdr.php) [function.require-once]: failed to open stream:

vineet

JasonDFR
12-01-2008, 06:54 AM
I should have asked what your directory structure is. I assumed "included_files" was under your public web root (htdocs).

If "included_files" is above, then go back to the way you were doing things, but when you place an image in html code, do what I suggested in my first reply <img src="/images/picture.jpg" />

Jason