PHP include pathing problems - php

I know this question has been asked plenty of times, or at least related questions, but I just can't seem to figure it out. I haven't done too much PHP programming, I'm just trying to keep a simple template across all of the website I'm working on.
Basically there is a sidebar,banner,footer.php. I just do
<?php include('includes/banner.php'); ?>
That is when you're in the root folder - in index.php.
Inside banner.php is:
<div id="banner">
<img src="images/../banner.jpg" width="1000" height="120" border="0">
</div>
That works fine and dandy for all the .php's in the root directory.
Now when I go to say directory A then to B I have another example.php ( So A/B/example.php ). Now if I try to use
<?php include('../../includes/banner.php'); ?>
It of course has the wrong path to the images. Since the code above is just pasted into the example.php with the image path of images/../banner.jpg. When it should be ../../images/../banner.jpg
How do I go about changing it so no matter how many directories deep I go, the banner image path is correct?
Just as FYI, I'm actually updating an old site for a friend. The layout of all the files and such ( and code, ugh ) are rather messy. So that's why I've been trying to clean it all up and make it a bit more organized and easier to update.
Thanks for help

This is the simple idea use in Joomla / Wordpress / Cakephp & other open source.
Use:
require( dirname( __ FILE __ ).'/images/imagename.jpg');
Let me know if this works.
Thanks,
Munjal

Related

include() doesn't work when my page has GET param

I have a header.php file containing my DOCTYPE and all my links/scripts.
I use
<?php
// HTML DOCTYPE insert
include 'header.php';
?>
at top of all my pages to have only one header for everyone, and it works fine.
Now, I have another page that get from a database a summary of my products information. When someone click on the "read more" link:
<p>
read more...
</p>
another page opens with the full information displayed...
Actually that works...
BUT on my new page (display_product.php/id=[anynumber]) my included file doesn't work. So I have no nav bar, no scripts, no stylesheet. Only the text from my database.
AND the weird thing is that when I copy/paste the HTML of my generated display_product page and launch it on my browser, it works... O-o
So the generated code is good.
AND the second weird thing is that when I get rid of the /?id... my layout works fine (but I have no text anymore, of course)
Does one of you have an idea why this crazy things happens?
"Hi guys, Thanks very much Fred -ii this was it. it works perfectly. Thanks sergiodebcn for your concerne."
Since other answers were given and did not solve the actual problem, am posting my comment to an answer, in order to close the question.
Remove the slash from /?id
The slash is trying to instruct the server to probably find a folder after a filename, which technically looks like is what's happening here.
The ultimate solution for include and require functions with path issues, is to use the absolute filesystem path to the file that you want to include or require.
i.e you may say:
include("C:\\www\\app\\incs\\header.php");
Hint
To learn how to set the absolute path for include dynamically for your project, check the source code of two files of cakephp framework:
index.php
webroot/index.php

Absolute paths in CakePHP project

i was given a pretty big cakePHP (built on v. 1.3.10) project to maintain. The problem is that the majority of the paths are absolute (which on my opinion is a bad habit).
Eg. in default.ctp there is:
<link rel="stylesheet" href="/css/public_new.css" />
but then at the bottom of the same file there is:
<?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?>
which prints the correct paths.
It's like the original developers made the site to put in a server's root (not in a subdirectory).
Things ive tried to solve this problem without success:
modified the .htaccess files on /, app/, and app/webroot
adding a tag
I know i can add a $this->base to the beginning of every path, but this is not a solution since there are thousands of files to modifiy :(
So my question: is there any solution using mod_rewrite or such?
Thanks in advance.
$html->script() will automatically prepend /js/ in the HTML output. Cake's .htaccess will then point these file calls to /webroot/js/.
So <?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?> would output
/js/jquery-ui-1.8.16.custom.min.js in the HTML.
To replace all those urls / links you could actually use a program like Actual Search and Replace ;-) I use it a lot.
Solution:
on app/config/boostrap.php, add:
Configure::write('App.base', '/teka_new/');
I take this out from Ho do I change the base path of routes in CakePHP?

PHP include doesn't display pictures

I have a small problem. I'm not good in php, so this may be something really simple. Basically, I have couple of websites, which I want to implement a specific picture on. I want it to be controlled (along with a hyperlink) from one script. So what I've done is this:
<?php include 'http://content.captive-portal.com/pd/sponsored/sponsored.php' ?>
I have placed this code on couple of websites but it doesn't display a picture. Why? Can it be done this way?
The page I placed this script on is this one:
amc link - the image should appear just underneath a logo. The gif image from the script above. Can anyone please help me out with this? What have I done wrong? can't it be done? THank you for your help.
The code is here:
<div id="wrapper">
<img id="logo" src="images/logotop_m.jpg"/>
<?php include 'http://content.captive-portal.com/pd/sponsored/sponsored.php' ?>
</div> <!--eo wrapper -->
...
Is the relative path causing the issue? Does the sponsored.php file make reference to the image using a complete path? (e.g.... http://content.captive-portal.com/images/.... or just images/...)
The reason for that is that some images are being used via relative links. Here's the logo:
<img id="logo" src="images/logotop_m.jpg"/>
As it isn't an absolute path with a domain, it'll resolve as the following:
http://yourdomain.com/images/logotop_m.jpg
Instead of what you're expecting:
http://amc.captive-portal.com/images/logotop_m.jpg
Edit: Oops
I didn't notice the redirect as I opened it in a new tab - my bad. The php file itself contains this:
<p><img src="http://content.captive-portal.com/pd/sponsored/sponsored.gif" width="480" height="80"></p>
As it's an absolute path, it *should* be alright.

How to use images and php includes across multiple directories?

I am brand new to PHP. I want to use it to include a universal header and footer in an html/jquery site. Currently I am using includes to do this:
<?php include('../includes/footer.php'); ?>
This works fine. Where I encounter a problem is with any images in the header or footer.
An explanation of my file structure:
Root folder: contains index.php and the folders "includes", "img", "php" etc.
php folder: contains gallery.php
includes folder: contains header.php, footer.php
When viewing the index.php all images in the header and footer show properly, but, because they are linked relatively (ex "img/facebook.png"), the do not show in gallery.php. To work they would need a ../ included. But then this would defeat the purpose of a universal header.
Thus I am trying to figure out how to link the images in the includes files in way that is doesn't matter where the php file is located. I have read this thread (which sounds like my problem) but I do not understand any of the answers. I have also read things that suggest $_SERVER['DOCUMENT_ROOT'] .'/folder/';, in conjunction with an echo to display the image. I tried this in my footer.php with this code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '/img/';
$image = ($path . "facebook.png");
echo "<img src=\"$image\" />"; ?>
When I view the page though, I end up with a little torn paper icon instead of my image. I assume this means that the path is incorrect, but I do not know why. facebook.png resides in the img folder. This problem occurs on both index.php and gallery.php.
After this rather long winded explanation (sorry), my mains questions are:
1) How do I get images to show up properly in my includes across multiple directories?
2) If I am going about this in the right way with the echo, what are the possible reasons why it is not working?
Once again, I know nothing about php, so if you could try to make your answers as basic as possible, it would be much appreciated.
Thank you!
Instead of img/facebook.png, add a / before the img/ like this: /img/facebook.png
This says "go to the root, and look for the img folder there. All your images should work fine then. The path of the images are absolute or relative based on the HTML page you're viewing, not which files you use to create it.
Though there's probably not much of reason for a "php" folder - just keep all your pages in the root directory.

PHP Link problem

I have a head file which I am using for a few different pages. The problem is when I go into a folder, the links in the head file point to say index.php instead of ../index.php
Is there any function to fix this or any work arounds that I'm missing?.
Thanks!
~ Kyle G
The links in the HTML? if so you can use absolute paths in your links. eg linking like so: <a href="/index.php">
If you're talking about include()/require() then one solution is to set your include path.
Or you could never serve html from subdirectories :) that's worked for me so far.

Categories