Web Analytics Made Easy -
StatCounter Path to images - image does not show - CodingForum

Announcement

Collapse
No announcement yet.

Path to images - image does not show

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Path to images - image does not show

    Hi,

    I have an include in my page with following code:
    PHP Code:
    <?php 
    $folder 
    '/images/header_images';
    $images glob$folder '/*.{jpg,JPG,gif,png}'GLOB_BRACE );
    $image  basename$imagesarray_rand$images ) ] );
    echo 
    '<a href="index.php?id=home"><img src="' $folder '/' $image '" alt="' .Header'" /></a>';  
    ?>
    It displays random images from a folder called 'header_images' located like this:
    Code:
    Root --> images --> header_images --> image.png
    But the image doesn't show on pages located in the root, neither on pages located outside the root.

    If I remove the leading slash in the $folder variable, the image does show in pages located in the server root. I want the image to show regardless of where the page is placed.

    Link: www.behrentzs.com/1.php
    Last edited by CaptainB; Apr 11, 2009, 02:46 PM.

  • #2
    First of all thanks a lot for such interesting functions glob and array_rand To get random elements I always used to use following construction: $array[mt_rand(0, count($array)-1)]

    As for the paths, I guess you have a problem with relative paths. Could you please support us with information in which directory the script is located and what is it's http url? That is very important information.
    Last edited by PHP6; Apr 11, 2009, 01:04 PM.

    Comment


    • #3
      You have to credit the guy who postet this in the PHP snippets forums - he's the master

      I have one main page in which I have an include(the above posted code). The page is located in my server root and the include is located as following:
      Code:
      Root --> includes --> include.php

      The images, which the script loads, is located as following:
      Code:
      Root --> images --> header_images --> image.png
      This is the code:
      PHP Code:
      <body>
      <div id="header">
      <?php include($_SERVER['DOCUMENT_ROOT']."/includes/header.php"); ?> 
      </div>
      ..........................
      ..........................
      However, the images, which the script should load, does not show. Have a look @ the link I posted - if you view the img src of the error 'cross' you see that the path is not somehow finished - it only refers to the header_images directory and not an actual image. That's what I don't understand.

      Comment


      • #4
        You have to credit the guy who postet this in the PHP snippets forums - he's the master
        The things is that every day I am still used to find some new functions or software constructions which makes me really happy

        As for your question:

        First of all your sample link does not work I do not know why but it shows me following error: No input file specified.

        Second - I want to be sure that if your domain is example.com and there is one file named test.jpg in /images/header_images direcotory then I could open from browser following url: http://example.com/images/header_images/test.jpg

        Waiting for your replay

        Comment


        • #5
          Doh, sorry - in the mean time I changed the url due to some reconstruction and moving. But here it is, should work www.behrentzs.com/index.php

          The images in the header_images folder are named head.png, head1.png, ...
          Last edited by CaptainB; Apr 11, 2009, 04:39 PM.

          Comment


          • #6
            Now I can see the page and I could say with 90% accuracy that you do not have problems with paths. I guess you have problems with CSS and HTML. Try to move that line:

            Code:
            <img src="/images/menu.png" id="button_menu" onMouseOver="this.src='/images/menu-hover.png';" onMouseOut="this.src='/images/menu.png';" alt="Click for menu!"/>
            outside of <div id="content"> tag, lets say move it right after <body> I am 100% you will see the images. If you will still have the same problem when just generate some sample HTML with only that line in the same directory so we will be sure that problem is in showing that images.

            Comment


            • #7
              Originally posted by PHP6 View Post
              Now I can see the page and I could say with 90% accuracy that you do not have problems with paths. I guess you have problems with CSS and HTML. Try to move that line:

              Code:
              <img src="/images/menu.png" id="button_menu" onMouseOver="this.src='/images/menu-hover.png';" onMouseOut="this.src='/images/menu.png';" alt="Click for menu!"/>
              outside of <div id="content"> tag, lets say move it right after <body> I am 100% you will see the images. If you will still have the same problem when just generate some sample HTML with only that line in the same directory so we will be sure that problem is in showing that images.
              But that line of code is just for the "Menu" image which actually shows.. it's the header images that doesn't display (the one which should be contained by the 'Header' div)

              Comment


              • #8
                It's strange - it's like it somehow 'strips' the filename of the image when a absolute path or a
                PHP Code:
                $_SERVER['DOCUMENT_ROOT']. 
                is inserted into the path of the image. It only gives me the path TO the header_images folder and NOT of any of the images in it

                Comment


                • #9
                  Originally posted by CaptainB View Post
                  But that line of code is just for the "Menu" image which actually shows.. it's the header images that doesn't display (the one which should be contained by the 'Header' div)
                  Sorry, that was my fault... I have no idea who I have managed to move from header to content div I guess now I know what is your problem. Just pay attention to the value you get:

                  Code:
                  <img src="/images/header_images/" alt="Header" />
                  in other words we have to check the value of $image variable. I guess it will be an empty string and I know why even more I could say that $images array will be empty too You need to specify not FTP root path of the directory where your images are located but actual OS root path.

                  Something like /home/username/www/images/header_images/ I do not think that you have /images/header_images/ such directory on the server

                  Comment


                  • #10
                    I think we're getting close now!
                    I tried echoing the $image and $images variables, and as you said they were empty !

                    Now, how would I find the correct root path? If I echo:
                    PHP Code:
                    $_SERVER['DOCUMENT_ROOT']; 
                    it gives me this:
                    /customers/behrentzs.com/behrentzs.com/httpd.www
                    But that's the FTP root path? Would my path then just be:
                    /customers/behrentzs.com/images/header_images

                    Comment


                    • #11
                      Everything depend which directory you see when you log in with FTP. I guess your home directory (I used to call it FTP root) will be /customers/behrentzs.com. Of cause if your images in FTP are located in the following directory /images/header_images your last path /customers/behrentzs.com/images/header_images will be the correct one.

                      BUT sometimes on Linux based hosting full paths could be blocked I had such situation one week ago the PWD command shows me the current directory (/home/username) which I use to generate full path (/scriptpath) but I cannot CD to script directory (/home/username/scriptpath) it shows me that such directory does not exist (I do not have enought permisions)

                      In that case there is a solution to use relative paths. if you script is located in /includes in FTP you can use following path ../images/header_images and it should work.

                      Comment


                      • #12
                        Ow, none of these worked:

                        /customers/behrentzs.com/images/header_images
                        /behrentzs.com/images/header_images
                        ../images/header_images

                        However, if it can help, this is what my FTP program says when I connect:

                        Respons: 230-User behrentzs.com has group access to: behrentz
                        Respons: 230 OK. Current restricted directory is /
                        Status: Connected

                        Comment


                        • #13
                          Great, at list we know where the problem is Try to use scandir() function with /behrentzs.com/images/header_images path and see if it is able to get contents of the directory.

                          Comment


                          • #14
                            From the server root, where the images folder is located, I can reach the header_images folder content with following:
                            /images/header_images/
                            I can not do that if the file is located OUTSIDE root of the server.

                            However, if I do THIS I can reach the files in the header_images folder when the file is IN or OUTSIDE the root:
                            $_SERVER['DOCUMENT_ROOT']./images/header_images/
                            None of these worked in the root (not tested outside the root):
                            /images/header_images
                            ../images/header_images
                            /behrentzs.com/images/header_images
                            http://www.behrentzs.com/images/header_images/
                            However, if I insert the above working into the script:
                            PHP Code:
                            $folder $_SERVER['DOCUMENT_ROOT'].'/images/header_images/'
                            The image still does not show - upen viewing the img src the new source is now:
                            (See for yourself here: www.behrentzs.com/index.php)
                            Last edited by CaptainB; Apr 12, 2009, 05:19 PM.

                            Comment

                            Working...
                            X