Page 1 of 2

Random Image issue with path to images

Posted: Wed Apr 07, 2010 7:09 pm
by WebPhenom
So I have code set up to grab random images as such:

<img src="<?php $_GET['folder']='../images/subpages/landscape/'; include 'includesPHP/random.php'; ?> width="350" height="150" alt="Random Image" /></div>

This issue is that the above code in itself is another include file on our site so that the path of the images will change depending on how deep inthe site you are. What I need to do is set up the path of the images to be set up like a virtual directory like we did for the random.php script itslef.

What is the best waht to do that? I tried just /images to be relative to the server root but no dice.

Posted: Wed Apr 07, 2010 9:04 pm
by Henrie
Why not use a absolute path (the full URL) to the image folder?

Posted: Wed Apr 07, 2010 9:26 pm
by WebPhenom
I would love to do that - the absolute path would fix this.

But when i try

<img src="<?php $_GET['folder']='/images/subpages/landscape/'; include 'includesPHP/random.php'; ?> width="350" height="150" alt="Random Image" />


for the GET folder part - it cant find the images even though that path is correct for a root relative path.

I cant use ../../ because while that may work for one page, it will not work on, say, a page that is one level deeper.. I would have to use ../../../ .

Because the image code is an include file itself I can drop it in many pages easily but need to find a way to have a path that works at any level, hence the server or site relative path using a / (or something similar that will work).

I must be coding something wrong cause this must work this way somehow, no?

Any ideas?

Posted: Wed Apr 07, 2010 9:35 pm
by Henrie
WebPhenom wrote:<img src="<?php $_GET['folder']='/images/subpages/landscape/'; include 'includesPHP/random.php'; ?> width="350" height="150" alt="Random Image" />
I meant an absolute path like:

Code: Select all

<img src="<?php $_GET['folder']='http://www.yoursite.com/images/subpages/landscape/'; include 'http://www.yoursite.com/includesPHP/random.php'; ?> width="350" height="150" alt="Random Image" />
Or maybe i misunderstand what you are trying to do.

Posted: Wed Apr 07, 2010 9:45 pm
by WebPhenom
I could not get that to work when i used a full absolute path like you mentioned.

Posted: Wed Apr 07, 2010 9:57 pm
by Henrie
Sorry, I don't have any other suggestion.

Regards,
Henrie

Posted: Thu Apr 08, 2010 7:21 pm
by WebPhenom
There should be a way to set this up with an absolute path though no? Anyone?

Posted: Thu Apr 08, 2010 8:03 pm
by Henrie
You are right, it should work with absolute paths. I may be stupid to ask, but you are certain the paths you entered are valid?

Could you post the php files (the random.php script and one page from which you call it) somewhere in a zip file so i can take a look at the code? Not sure when I have time to look at it, but when it is not available, i am sure i can not help you any more.

Regards,
Henrie

Posted: Thu Apr 08, 2010 9:59 pm
by WebPhenom
OK thanks. Here is a zip with those 2 pieces at webdev2.tmcc.edu/dev/desktop.zip
I am on a windows server running PHP too if that matters.

Thanks!

Posted: Thu Apr 08, 2010 11:53 pm
by Henrie
I looked at it. I never used the random image script myself.

Could you add following as first line to webservices.php, this will display errors if present.

Code: Select all

<?php ini_set('display_errors', 1); ?>
Also please create a file includetest.php with following content:

Code: Select all

<?php echo 'This is content of includetest.php file displayed'; ?>
And add following to webservices.php (change paths to what you think are the correct relative and absolute paths to the includetest.php file).

Code: Select all

<?php 
echo '<hr>relative link to includetest.php (result on next line):<br>';
include '../includetest.php'; 
echo '<hr>absolute (full url) link to includetest.php (result on next line):<br>';
include 'http://127.0.0.1/includetest.php'; 
?>
Please let me know if you get any errors.
On my own local running server I get an error with the full url, my php default setting is to not accept URL file-access for php include function.

Greetings,
Henrie

Posted: Fri Apr 09, 2010 2:02 am
by WebPhenom
i did get an error for the absolute include.

Code: Select all


<?php
echo '<hr>relative link to includetest.php (result on next line):<br>';
include '../../includetest.php';
echo '<hr>absolute (full url) link to includetest.php (result on next line):<br>';
include '/includetest.php';
?>


Warning: include(/includetest.php) [function.include]: failed to open stream: No such file or directory in C:\Inetpub\wwwroot\includesPHP\headers\webservices.php on line 15

Warning: include() [function.include]: Failed opening '/includetest.php' for inclusion (include_path='.;c:\inetpub\wwwroot') in C:\Inetpub\wwwroot\includesPHP\headers\webservices.php on line 15
IN my ini file i have include_path='.;c:\inetpub\wwwroot' this allows me to access includes like this

Code: Select all

include 'includes/test.php'; 
but i cant get the the server root / which is where i placed the includetest.php file.

Is the issue I need to set up my PHP enviroment to access the server root / and if so, how?

Posted: Fri Apr 09, 2010 6:09 pm
by Klemen
The error mesasge you get says it all: "failed to open stream: No such file or directory"

It means your folder settings are wrong. Try using he FULL paths in includes, I mean like include 'C:\Inetpub\wwwroot\includesPHP\headers\script.php';

Posted: Fri Apr 09, 2010 6:10 pm
by Henrie
I am sorry, my knowledge of configuring php on the server is not so good that i can help you with this.
Seems your knowledge is already bigger than mine :roll:

Greetings,
Henrie

Edit: lol, it seems Klemen and I were writing at the same time. He just pushed Submit a little earlier than me.

Posted: Fri Apr 09, 2010 6:56 pm
by WebPhenom
yes i can get the include working using that or server document root :

Code: Select all

include ($_SERVER['DOCUMENT_ROOT'].'/includetest.php');
but i still am not able to get the random image to accept an absolute page for the get folder. Ieven tried:

Code: Select all

<?php $_GET['folder']= ($_SERVER['DOCUMENT_ROOT'] . "/images/subpages/landscape/"); include ($_SERVER['DOCUMENT_ROOT'] . "/includesPHP/randomimage.php"); ?>"
This pulls in the include file but I am getting a broken image now as the image path now:

Code: Select all

<img src="c:\inetpub\wwwroot/images/subpages/landscape/image098.jpg"
Doing it as just /images/subpages/landscape/ gives me the NO IMAGE icon.

I tried every way i cant think of to get it to work with an absolute or site/root relative path and no dice. It seems to only with with page relative links like ../../ etc.

I am just trying to get the override working because our images are in many places and I want to be able to grab them from the SRC i specify on each page. This code seemed the perfect solution if only we could get the absolute path working for GET Folder variable.

Posted: Fri Apr 09, 2010 7:24 pm
by Henrie
the <img src="c:\inetpub\wwwroot/images/subpages/landscape/image098.jpg"> src path must be accessible from the webbrowser. This usually means it should be a path like "http://www.youriste.com/images/subpages ... age098.jpg"