Web Analytics Made Easy -
StatCounter Help with Relative Image URL in PHP - CodingForum

Announcement

Collapse
No announcement yet.

Help with Relative Image URL in PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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 Files
    Last edited by Psionicsin; Aug 25, 2011, 01:32 AM.

  • #2
    Would I do...?

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

    Comment


    • #3
      Can anyone steer me in the right direction here?

      Comment


      • #4
        <img src=\"/images/sencha/" .$row['pic_url']. "\"/>

        Comment


        • #5
          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; Aug 25, 2011, 03:24 PM.

          Comment


          • #6
            NightFire or anyone?

            Comment


            • #7
              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. ^_^

              Comment


              • #8
                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; Aug 26, 2011, 11:53 PM.

                Comment


                • #9
                  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']. "\"/>" ?>

                  Comment


                  • #10
                    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. ^_^

                    Comment


                    • #11
                      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.
                      PHP Code:
                      header('HTTP/1.1 420 Enhance Your Calm'); 
                      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                      Comment


                      • #12
                        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"?

                        Comment


                        • #13
                          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; Aug 29, 2011, 01:27 PM.

                          Comment


                          • #14
                            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.
                            PHP Code:
                            header('HTTP/1.1 420 Enhance Your Calm'); 
                            Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                            Comment


                            • #15
                              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

                              Comment

                              Working...
                              X