Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, On my web page I have a search function that searches my database for relative matches. I have pictures associated with each item in the database stored in my images directory on the server. The search calls a CGI script that displays the contents of the database relavent to the search and it needs to look in my images directory to see if the image exists. If the images exists i want to display that image, but if it does not, then I have a default image I want to display. I create the path out of variables to use in a "if" statement.

my $path="http://servername.com/username/images/"; my $imagename="filename.jpg"; my $imagedefault="default.jpg"; if ( -e '$path.$imagename'){ print "<"."code><td><IMG SRC=."$path.$imagename."></td></code".">"; } else { print "<" . "code><td><IMG SRC=".$path.$imagedefault."></td></code" ." +>"; }


I have verified that the images will print, and I have printed out the path from the variables and that it correct by doing.

print $path.$imagedefault;

And this displays the exact path that I use in the IMG tag and and the images displays just fine.

The problem is that: if ( -e '$path.$imagename') does not see that the file exists, so it always uses the default images, even if an image exists.

So, I am running everything from the same server and I have a CGI script trying to chack that a files exists. How can I do this? I have tried everything I can think of and it appears that I am doing what other threads have suggested, but no luck.

Thanks in advance!!!!

20031210 Edit by Corion: Added code tags

Replies are listed 'Best First'.
•Re: How to check If file exists?
by merlyn (Sage) on Dec 09, 2003 at 21:46 UTC
      Thanks for all of the replies, Yes, I have tried single quotes, double quotes, no quotes, not "dots", and they all don't work.

      I'm not sure what you mean? How would it know where to look for the file?
      The -e operation works on filenames, not URLs. Give it the filename that will be served by that URL instead.

      This sounds like it might works, but I am unlear how do do this.
      Thanks!
        Thanks All! I got it to work with your help. Turns out I needed to take off the ticks, which I had tried before, then I used just the path name like you suggested and not the URL. I was getting the two mixed up. Thanks for pointing that out!!
Re: How to check If file exists?
by Enlil (Parson) on Dec 09, 2003 at 21:34 UTC
    Does it find the file if you change:
    if ( -e '$path.$imagename')
    to:
    if ( -e "$path.$imagename")
    as single quotes won't interpolate and you are thus looking for a file called: $path.$imagename.

    update: upon looking at this further you will probably want to drop the dot inside the quotes, as the dot does not concatenate inside quotes either.

    update2: Seems like b10m++ mentioned this below as I was updating.

    update3:I missed the fact that you are looking at a url rather than a file... as merlyn's answer below points out...

    -enlil

      Does your database reside on a different server than your website? If not why have the file path be a URL? Why not a local file path?

      And yes one of your issues is the single ticks. You should really always try to bring at least the concantenation character (.) out of any string literal.

      if (-e $path.$imagename) {}
      or if you really need to quote the variables use:
      if (-e "$path"."imagename") {}
      Unless you (OP) have a dot in front of all the filenames (possible,but not likely), you'd probably want to drop the dot.
      if ( -e "$path$imagename")
      Update: As Enlil, I too missed the URL fact...

      --
      B10m