Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts

    Help with Relative Image URL in PHP

    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?
    Attached Thumbnails Attached Thumbnails Help with Relative Image URL in PHP-directory.png  
    Last edited by Psionicsin; 08-25-2011 at 05:32 AM.

  • #2
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Would I do...?

    PHP Code:
    <?php echo "<img src=\"$_SERVER['DOCUMENT_ROOT'] . '$row[pic_url]''" ?>
    Last edited by Psionicsin; 08-24-2011 at 10:25 PM.

  • #3
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Can anyone steer me in the right direction here?

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    <img src=\"/images/sencha/" .$row['pic_url']. "\"/>

  • #5
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    <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 Code:
    <?php echo "<img src=\"/images/sencha/" .$row['pic_url']. "\"/>" ?>
    It's spitting this out
    PHP Code:
    <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.
    Last edited by Psionicsin; 08-25-2011 at 07:24 PM.

  • #6
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    NightFire or anyone?

  • #7
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    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.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #8
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Chris Hick View Post
    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 Code:
    <?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???
    Last edited by Psionicsin; 08-27-2011 at 03:53 AM.

  • #9
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    I'm able to get it to show using the absolute URL of the image like:
    PHP Code:
    <?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 Code:
    <?php echo "<img src=\"INSERT_CODE_RELATIVE_TO_CURRENT_DIRECTORY" .$row['pic_url']. "\"/>" ?>

  • #10
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Have you not already applied .. to the url such as this?

    PHP Code:
    <?php echo "<img src=\".." .$row['pic_url']. "\"/>" ?>
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • Users who have thanked Chris Hick for this post:

    Psionicsin (08-29-2011)

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #12
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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 Code:
    <?php echo "<img src=\"http://www.rutholsonphoto.com" .$row['pic_url']. "\" width=\"100%\" />" ?>
    and nothing in PHP can replace the "www.rutholsonphoto.com"?

  • #13
    Regular Coder Psionicsin's Avatar
    Join Date
    Aug 2010
    Location
    Detroit, Michigan
    Posts
    346
    Thanks
    54
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Chris Hick View Post
    Have you not already applied .. to the url such as this?

    PHP Code:
    <?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 Code:
    <?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.
    Last edited by Psionicsin; 08-29-2011 at 05:27 PM.

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #15
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Quote Originally Posted by Psionicsin View Post
    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 Code:
    <?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 Code:
    <?php echo "<img src=\"..\" .$row['pic_url']. ""/>" ?>
    is the proper way, not the way you did it


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •