When a customer is viewing any given product page, under the product picture, I have a little magnifying glass along with the text "Click here to view a larger image".

Now, currently, for EACH product, I have a field in the database that says whether or not a product has a larger image on file.

So then on the products.php page, I just do a little if statement to check what the value is of that field. If it is a "y", then I code the site with the magnifying class and the appropriate text. Otherwise, nothing is shown underneath the image.

My question is, is there a way for PHP to do this for me instead of having to keep track, in a database, of whcih products have larger images, and which do not?

Can php somehow do a quick check on the server for a given file, and if it is there, do some code. If not, do something else...or in my case, do nothing?

Thanks.

    use [man]file_exists[/man] to see if there is a large image

      darn it. You beat me to it. I just found the file_exists in the manual.

      Here's the example it gave:

      <?php
      $filename = '/path/to/foo.txt';
      
      if (file_exists($filename)) {
         echo "The file $filename exists";
      } else {
         echo "The file $filename does not exist";
      }
      ?>

      My question is, does the $filename have to include the EXACT path with the http://SERVER/images/picturelarge.jpg? Or does the path in the $filename start on your server?

      Ideally, I would code it as $filename = '/images/picturelarge.jpg'

      Should that work? Cause i'm getting me an error.

        Even when i type in my "server" (localhost) manually, the file exists is not working properly.

        Here's my snippet:

        $filename = "http://localhost/images/prod_cat/" . $myrow["catID"] . "/" . $myrow["subcatID"] . "/". $myrow["item_num"] . "b.jpg";
        echo $filename;
        if (file_exists($filename)) {
        	// do fun stuff
        }
        else {
        	// do fun stuff
        }

        I'm only echoing the filename to confirm that my coding was correct. The filename does indeed point to a file on the server. However, the ELSE statement is what is executed.

        Any ideas? I am reading online at the php link you gave me and a poster noted at the bottom that the file_exists is failing on most images under 2GB on x86 platforms. So does this function work?

          Try $filename = './images/picturelarge.jpg'
          Notice the dot before the first slash.
          If that doesn't work chmodd the "images" directory to "777".

            how do I chmodd the images directory on my own computer?

            sorry for a dumb question.

              The "." before the slash does not do anything different. I also tried the is_file() function with no success.

                yahyac wrote:

                how do I chmodd the images directory on my own computer?

                sorry for a dumb question.

                You edited your post. I didn't know it was on your home PC.

                  hmmm try $filename = 'images/picturelarge.jpg';

                  I don't feel like reproducing this so I'm just throwing you suggestions.

                    Again, when I try the ACTUAL PATH, it still doesn't work.

                    $filename = "http://localhost/images/prod_cat/" . $myrow["catID"] . "/" . $myrow["subcatID"] . "/". $myrow["item_num"] . "b.jpg"; 
                    echo $filename; 
                    if (file_exists($filename)) { 
                        // do fun stuff 
                    } 
                    else { 
                        // do fun stuff 
                    }

                    This is NOT working. So if the full path is not working, it would be surprising if anything less would.

                    So now it is a matter of why.

                      Try this code

                       $filename = "images/prod_cat/" . $myrow["catID"] . "/" . $myrow["subcatID"] . "/". $myrow["item_num"] . "b.jpg";
                      echo $filename;
                      if (file_exists($filename)) {
                          // do fun stuff
                      }
                      else {
                          // do fun stuff 

                      I've tested it and it works
                      It doesn't work with the full URL though
                      Here's my code

                      $filename = "images/desktop.png";
                      echo $filename;
                      if (file_exists($filename)) 
                      {
                          echo "yes";
                      }
                      else 
                      {
                          echo "no";
                      }

                        That still didn't work. If i read you correct, the only thing you changed was the:
                        $filename = "images/prod_cat/"

                        That change does not work, at least on my system. Maybe it works for you because you are calling this page from from a directory just above "images". So it logically just steps down into images and so forth.

                          Okay, now it IS working, using the standard unix code. The page I am calling from is on the same "level" as the images directory. So just for the heck of it, i tried;

                          $filename = "../images/prod_cat/" . $myrow["catID"] . "/" . $myrow["subcatID"] . "/". $myrow["item_num"] . "b.jpg"; 
                          echo $filename; 
                          if (file_exists($filename)) { 
                              // do fun stuff 
                          } 
                          else { 
                              // do fun stuff 
                          }

                          And it works!

                          So this is just weird how php acts sometimes. Sometimes it wants the direct address. Other times the concept of using just a slash to start the link works ("/images/prod_cat/"), as this tells it to go to the root of the server and then go to images and so forth. Other times, as in this case, php wanted the unix style. Weird.

                            So you had to go back one directory to get it.
                            I didn't think of asking that.

                              It's not that I couldn't think of that, as that is how i have always coded. But since starting php this past week or so, I started using just a "/" at the beginning of all links, as when parsed, it understand this to mean "start at the root of the server".

                              Why that did not work here? no clue. Why the full path did not work either? no clue.

                              But at least it works and I know how ti use the file_exists in the future.

                              Thanks for your help.

                                The reason it didn't work is because the file that you were using for the file_exists() method wasn't sitting in root.

                                For example say you had the code that you were trying to fix in http://localhost/test/fileexists.php

                                When you use $filename = "images/boo.jpg"; it adds that extension on to your URL thus making it http://localhost/test/images/boo.jpg".

                                When you used the command to go back a directory ($filename = "../images/boo.jpg";) it got the image from http://localhost/images/boo.jpg

                                I hope this helps better than my previous posts 🙂

                                  Sure, now that I know that only the unix style of going up a root works with the file_exists, it makes sense. Mind you, as mentioned, I always code, in html, using those type of unix, relative paths.

                                  The confusion was not on how to setup that path. But rather, on why the other methods of linking were not working. <img src="/images/boo.jpg"> works from any level of your site...any level. Because the first "/" says "go to the root".

                                  So similarly, I expected $filename = "/images/boo.jpg"; to do the same thing. I did not. No big deal.

                                  Again, thanks for your help.

                                    Write a Reply...