...

View Full Version : Check if Image exists



holty
10-13-2003, 12:19 PM
Hi,

I'm passing through a customers id through a form.

I have images stored of the customer named by their id in an 'images' folder.

So - Customer ID '1' has a image 'images/1.jpg'.

I can display the customers image by doing:

<img src="images/<%= CustomerID %>" width="140" height="100"></td>

where ID is:
CustomerID = request.querystring("ID") & ".jpg"

My question is - if the image doesn't exist in the images folder - can I show some text saying 'Image Required' instead of the little cross?

Any help would be great :)

raf
10-13-2003, 02:25 PM
You can use the FSO fileexists method:
http://www.devguru.com/Technologies/vbscript/quickref/filesystemobject_fileexists.html

holty
10-13-2003, 03:01 PM
Raf,

I'm trying to use that like this :

FUNCTION FileExist(FileName)
Dim FS, DoesExist
DoesExist = FALSE
SET FS=Server.CreateObject("Scripting.FileSystemObject")
IF (FS.FileExists(FileName)) = TRUE THEN
DoesExist = TRUE
END IF
SET FS=Nothing
FileExist = DoesExist
END FUNCTION


<% IF FileExist(server.mappath("images") & "CustomerID") THEN %>
<img src="images/<%= CustomerID %>" width="140" height="100"></td>
<% else %>
no file exists</td>
<% end if %>


But it doesn't like it - if you look at my previous post the CustomerID is appended to a '.jpg'

can you see what i've doen wrong?

raf
10-13-2003, 04:25 PM
Maybe try something like


dim fileadr
fileadr = "../images/" & request.querystring("ID") & ".jpg"
' comment : or "images/" & request.querystring("ID") & ".jpg" --> depending on where images is situated, relativaly to this file

IF FileExist(server.mappath(fileadr) THEN
response.write("<img src="""& fileadr & """ width=""140"" height=""100"">")
else
response.write("no file exists")
end if
response.write("</td>")

holty
10-13-2003, 04:55 PM
Raf - got that working!!

Cheers mate - you've been a great help!

raf
10-13-2003, 10:31 PM
You're welcome. Glad you got it working.

whammy
10-14-2003, 03:22 AM
By the way, glenngv and I came up with a pretty cool client-side solution to this too (whether or not an image (or webpage!) exists, and ways to deal with using client-side javascript)...

I suggested the idea, and he coded it perfectly (exactly what I was thinking!), if you can find it. :)

glenngv
10-14-2003, 08:35 AM
http://www.codingforums.com/showthread.php?s=&threadid=23725

:)

whammy
10-15-2003, 05:01 AM
Brilliant.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum