...

View Full Version : Help with Relative Image URL in PHP



Psionicsin
08-24-2011, 09:02 PM
I can't figure out how to get the relative image url to the photo of the person. The mobile site (see attachment) is in a folder (highlighted green) inside of the root of the site, and the images of the people are in the root images folder (highlighted in red), and NOT the mobile images folder. How do I tell "$row[pic_url]" to go back to the root folder before executing the command?

Psionicsin
08-24-2011, 10:21 PM
Would I do...?


<?php echo "<img src=\"$_SERVER['DOCUMENT_ROOT'] . '$row[pic_url]''" ?>

Psionicsin
08-25-2011, 05:32 AM
Can anyone steer me in the right direction here?

Nightfire
08-25-2011, 05:40 AM
<img src=\"/images/sencha/" .$row['pic_url']. "\"/>

Psionicsin
08-25-2011, 07:11 PM
<img src=\"/images/sencha/" .$row['pic_url']. "\"/>

Alright now can you explain to me exactly how this works? Coding to English. I'm not this advanced yet, and love to learn.

And I believe I implemented your code wrong as I'm getting an error. I did this:
<?php echo "<img src=\"/images/sencha/" .$row['pic_url']. "\"/>" ?>

It's spitting this out
<img src="/images/sencha//images/sencha/garban-gc.jpg"/>

...and that's not right for the path of the images. It's supposed to go back one directory into the root of the server.

Psionicsin
08-26-2011, 11:13 PM
NightFire or anyone?

Chris Hick
08-27-2011, 12:41 AM
That is because inside your $row['pic_url'] you have put the whole url path in it. You just need to have the image name within it, then use what NightFire suggested.

Psionicsin
08-27-2011, 03:45 AM
That is because inside your $row['pic_url'] you have put the whole url path in it. You just need to have the image name within it, then use what NightFire suggested.

Correct me if I'm wrong, but I'm not sure if that's something I can change do to the way the code currently functions. Here's the full code as is thus far:


<?php
require_once('templates/mysql_connect.php');
?>

<?php
$query = "SELECT * FROM seniors WHERE sen_id=$_GET[sen_id]";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result);
?>

<div class="contents">
<h1><?php echo $row['first_name'], " ", $row['last_name']; ?></h1>
<h4><?php echo $row['school']; ?></h4>
</div>

<?php echo "<img src=\"/images/sencha/" .$row['pic_url']. "\"/>" ?> <br />
<br />
<a href="/">{{VOTEBUTTON}}</a>


In our DB, the field "pic_url" holds the value "/images/sencha/"image-name".jpg". I cannot, and will not, change that value as it's something that has been set in stone and would completely disrupt the contest currently running. So there has to be another way to do this that won't disrupt the contest, yet still give me the results I need. I thought it would be as simple as adding in a ".." as that does, to my knowledge, mean for that line of code to back out one directory before searching for the file.

So you guys are telling me that there's absolutely no code to add that will make it back out one directory BEFORE searching for the file???

Psionicsin
08-27-2011, 03:57 AM
I'm able to get it to show using the absolute URL of the image like:

<?php echo "<img src=\"http://www.rutholsonphoto.com" .$row['pic_url']. "\"/>" ?>

But I'd MUCH RATHER use some type of code that uses the relative address like:

<?php echo "<img src=\"INSERT_CODE_RELATIVE_TO_CURRENT_DIRECTORY" .$row['pic_url']. "\"/>" ?>

Chris Hick
08-28-2011, 10:52 PM
Have you not already applied .. to the url such as this?



<?php echo "<img src=\".." .$row['pic_url']. "\"/>" ?>

Fou-Lu
08-29-2011, 02:01 AM
You do not need this to be relative.
HTML paths work different than PHP ones. PHP being server side can access the filesystem, while HTML only access the web root and beyond. So if you have an images directory in http://site.com/images, you can access it literally from /images. In PHP, you cannot do this, as it would have to go from your document root.

Psionicsin
08-29-2011, 05:17 PM
You do not need this to be relative.
HTML paths work different than PHP ones. PHP being server side can access the filesystem, while HTML only access the web root and beyond. So if you have an images directory in http://site.com/images, you can access it literally from /images. In PHP, you cannot do this, as it would have to go from your document root.

So, just to make sure I'm 100% on the same page with you..

HTML can go back as many directories as needed using combinations of "../../..etc", yet PHP has no function to make it back out of a directory 1 or more times?

"$row['pic_url'];" is equal to "images/sencha/pic_name.jpg", and this cannot be changed. So I'm stuck with having the absolute URL via
<?php echo "<img src=\"http://www.rutholsonphoto.com" .$row['pic_url']. "\" width=\"100%\" />" ?> and nothing in PHP can replace the "www.rutholsonphoto.com"?

Psionicsin
08-29-2011, 05:21 PM
Have you not already applied .. to the url such as this?



<?php echo "<img src=\".." .$row['pic_url']. "\"/>" ?>


Ok Chris Hick, thank you for this. Since CSS that's housed in a PHP had to be formatted like field=\"value\", I assumed that the proper code should've been
<?php echo "<img src=\"..\" .$row['pic_url']. "\"/>" ?>

I never would've imagined that I'd have to remove that last "\" to make it work. Based on others responses, I thought I was asking something impossible or something. Thank you for showing me the PROPER format of this.

Fou-Lu
08-29-2011, 06:29 PM
No they are both capable of relative. What I'm saying is, if document root is at /home/username/public_html, PHP can access anything in /home if its permissions allow, or /var or / if you can, but HTML is completely jailed to /home/username/public_html as its lowest root path.
I wouldn't replace that url at all, I just wouldn't use it. If your images directory is directly off of your public_html or whatever the document root is, you can simply use / followed by your image path. Using an absolute in HTML makes a lot of sense since you know where the root is located at all times.

Nightfire
08-29-2011, 06:46 PM
Ok Chris Hick, thank you for this. Since CSS that's housed in a PHP had to be formatted like field=\"value\", I assumed that the proper code should've been
<?php echo "<img src=\"..\" .$row['pic_url']. "\"/>" ?>

I never would've imagined that I'd have to remove that last "\" to make it work. Based on others responses, I thought I was asking something impossible or something. Thank you for showing me the PROPER format of this.

He showed you the proper way, then you just comlpetely changed it and that way won't work lol

<?php echo "<img src=\"..\" .$row['pic_url']. ""/>" ?> is the proper way, not the way you did it

Psionicsin
08-29-2011, 06:50 PM
He showed you the proper way, then you just comlpetely changed it and that way won't work lol

<?php echo "<img src=\"..\" .$row['pic_url']. ""/>" ?> is the proper way, not the way you did it

But...that is the way I did it lol. When I did it like what you have up above, it didn't want to show. When I did it the way Chris typed out, it worked perfectly.


src=\"..\" Didn't work.

src=\".." Worked.

And, not sure how you interpreted my response, but I was praising Chris, not blaming him. He, indeed, gave me the answer I was looking for lol. Hopefully it came off that way.

Fou-Lu
08-29-2011, 07:20 PM
He showed you the proper way, then you just comlpetely changed it and that way won't work lol

<?php echo "<img src=\"..\" .$row['pic_url']. ""/>" ?> is the proper way, not the way you did it

No this isn't right. That has improper escape sequences within it; \" is trying to escape the " as a literal char, so when used with ..\" . $row['pic_url'] it essentially leaves . within the charpath surrounding $row['pic_url']. What would work is:


<?php echo '<img src="../' . $row['pic_url'] . '" alt="" />'; ?>

Psionicsin
08-29-2011, 07:50 PM
What would work is:


<?php echo '<img src="../' . $row['pic_url'] . '" alt="" />'; ?>


Ok Fou-Lu, I used what you typed as a base and have:

<?php echo '<img src="../' . $row['pic_url'] . '" width=\"100%\" alt="Senior Challenge Photo" />'; ?>

But I'm gettign errors with this.

Fou-Lu
08-29-2011, 08:26 PM
That's not the same as I have.


<?php echo '<img src="../' . $row['pic_url'] . '" width="100%" alt="Senior Challange Photo" />';?>


Posting errors would sure be nice.

Psionicsin
08-29-2011, 08:36 PM
That's not the same as I have.


<?php echo '<img src="../' . $row['pic_url'] . '" width="100%" alt="Senior Challange Photo" />';?>


Posting errors would sure be nice.

Not an error, so much as the photo isn't showing anymore. HERE'S THE PAGE (http://m.rutholsonphoto.com/vote.php?sen_id=669)

Fou-Lu
08-29-2011, 08:37 PM
Then your path is simply wrong.
Images folder doesn't exist one level up from where you are currently sitting.

Psionicsin
08-29-2011, 08:43 PM
Then your path is simply wrong.
Images folder doesn't exist one level up from where you are currently sitting.

The images folder sits at "http://www.rutholsonphoto.com/images". The mobile folder is "http://www.rutholsonphoto.com/rop-mobile". All PHP files in the mobile folder are directly in the root of that folder, so backing up one directory should make it look into "http://www.rutholsonphoto.com/images".

Fou-Lu
08-29-2011, 08:47 PM
It sure should. What is the html output if these file paths?

Psionicsin
08-29-2011, 08:52 PM
It sure should. What is the html output if these file paths?

The source output is:

<img src="..//images/sencha/garban-gc.jpg" width="100%" alt="Senior Challange Photo" />

However I just thought of something. The main site URL is rutholsonphoto.com. The mobile site URL is m.rutholsonphoto.com. If I view the mobile site via the absolute URL, which is "www.rutholsonphoto.com/rop-mobile/", the pictures show up.

So no matter how many hyphens and slashes I put in there, it's always going to use m.rutholsonphoto.com as the root because that's in the URL bar. So...I HAVE to absolute URL link the photos, then?

Fou-Lu
08-29-2011, 08:59 PM
Yes you do. Unless you have a copy of /images into m.domain.com, the subdomain cannot reach the primary domain. That I can see being the primary reasons for using an absolute DNS resolved path.
On the other hand, I'm not sure if it will work but you can attempt to create a sybmolic link to /images from your subdomain to your primary domain's /images path. That may resolve as expected; a test would confirm or deny that.

You can get your dns under $_SERVER['SERVER_NAME'];. I believe that will resolve the subdomain as well; if not just dump the $_SERVER to find which offset does.

Psionicsin
08-29-2011, 09:02 PM
Yes you do. Unless you have a copy of /images into m.domain.com, the subdomain cannot reach the primary domain. That I can see being the primary reasons for using an absolute DNS resolved path.
On the other hand, I'm not sure if it will work but you can attempt to create a sybmolic link to /images from your subdomain to your primary domain's /images path. That may resolve as expected; a test would confirm or deny that.

You can get your dns under $_SERVER['SERVER_NAME'];. I believe that will resolve the subdomain as well; if not just dump the $_SERVER to find which offset does.

Thanks for that, but I have no idea how to do any of that lol. How does that work, and where do I utilize it?

Fou-Lu
08-29-2011, 09:29 PM
Thanks for that, but I have no idea how to do any of that lol. How does that work, and where do I utilize it?

You would establish that on the filesystem of the server. Assuming its a linux machine you could execute a command line call to set up a link with ln -s /path/to/original /path/to/link, which would represent your absolute path to the original images folder linked to your subdomain folder.
You may have a way to establish this in a control panel of some sorts.

Psionicsin
08-29-2011, 09:41 PM
You would establish that on the filesystem of the server. Assuming its a linux machine you could execute a command line call to set up a link with ln -s /path/to/original /path/to/link, which would represent your absolute path to the original images folder linked to your subdomain folder.
You may have a way to establish this in a control panel of some sorts.

Yes, Linux based (hosted through FatCow). But again, I have no idea what you're talking about. I'll just leave it as an absolute as it seems easier and less likely to make me cry lol.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum