www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Images in directory

  1. #1
    Join Date
    May 2003
    Location
    UK
    Posts
    220

    Images in directory

    Hi there,

    I need to display all the images in a directory, inside a table no more that 4 columns wide (but I would like it to be easily customizable if I need to change it). It can be any number of rows, obviously depending on how many images there are.

    The images will be stored in a different folder to the .asp page.

    Thanks very much/...

  2. #2
    Join Date
    Jun 2005
    Location
    WI, US
    Posts
    188
    are you pulling a recordset that has image filenames in it? if so, you could do it like this

    <table>
    <% do while not rs.EOF %>
    <tr>
    <td><img src="/directory/<%=rs("filename")%>"</td>
    <% rs.MoveNext %>
    <td><img src="/directory/<%=rs("filename")%>"</td>
    <% rs.MoveNext %>
    <td><img src="/directory/<%=rs("filename")%>"</td>
    <% rs.MoveNext %>
    <td><img src="/directory/<%=rs("filename")%>"</td>
    </tr>
    <% loop %>
    </table>

  3. #3
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    Here is some bare bones code that you can use to traverse you loop and display your images. This is set up for 4 columns and will go for ever. In our case it will only loop untill the For Next loop is satisified. If you want more columns just increase the i MOD 5 to something else. The number of columns will be 1 less than the # after MOD.

    If your images are not in a Database then you should be able to use FSO (FileSystemObject) to traverse your directory. Here is a link to a quick reference of FSO
    http://www.tutorial-web.com/asp/fso/drive.asp
    Code:
    <%
    option explicit
    
    Dim i 
    
    Response.Write "<table border=1>"
    Response.Write "<tr>"
    For i = 0 To 10 
    	If i Mod 5 Then 
    		Response.Write "<td>TEST</td>"
    	Else
    		Response.Write "</tr><tr>"
    	End if 
    Next 
    Response.Write "</table> "
    %>

  4. #4
    Join Date
    May 2003
    Location
    UK
    Posts
    220
    I've managed to come up with the following code, but can't seem to get it to spread over 4 columns.

    Code:
    <% 
        Dim strThisPage 
        strThisPage = Request.ServerVariables("SCRIPT_NAME") 
        strThisPage = Right(strThisPage, Len(strThisPage) - 1) 
         
        'Path To Folder That holds Files To Download Here 
        'Default is the current Folder 
        FILE_FOLDER = StripFileName(Request.ServerVariables("PATH_TRANSLATED")) 
    %> 
    <html> 
    <head> 
        <title>Images</title> 
        <style type="text/css"> 
        .TabHeader { Font-Family: Arial; Font-Weight: Bold; Font-Size: 12px; Background: Silver } 
        .DataCol { Font-Family: Verdana; Font-Size: 12px } 
        </style> 
    </head> 
    
    <body> 
    <table id=tblFileData background=""> 
        <tr> 
            <td class=TabHeader></td> 
        </tr> 
    <% 
        Call GetAllFiles() 
    %> 
    
    
    </table> 
    </body> 
    </html> 
    <% 
    Sub GetAllFiles() 
        Dim oFS, oFolder, oFile 
        Set oFS = Server.CreateObject("Scripting.FileSystemObject") 
             
        'Set Folder Object To Proper File Directory 
        Set oFolder = oFS.getFolder(FILE_FOLDER) 
         
        Dim intCounter 
         
        intCounter = 0 
         
            Dim FileArray() 
            ReDim Preserve FileArray(oFolder.Files.Count, 1) 
         
            For Each oFile in oFolder.Files 
                strFileName = oFile.Name 
                     
                FileArray(intCounter, 0) = strFileName 
                FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>" 
             
                intCounter = (intCounter + 1) 
            Next 
             
            intRows = uBound(FileArray, 1) 
            intCols = uBound(FileArray, 2) 
         
            For x = 0 To intRows -1 
                Echo("<tr>") 
                For z = 0 To intCols 
                    If z > 0 Then 
                        BuildTableCol(FileArray(x, z)) 
                    End If 
                Next 
                Echo("</tr>") 
            Next 
             
        EchoB("<B>" & oFolder.Files.Count & " Files Available</B>") 
             
        Cleanup oFile 
        Cleanup oFolder 
        Cleanup oFS 
    End Sub 
    
    Function Echo(str) 
        Echo = Response.Write(str & vbCrLf) 
    End Function 
    
    Function EchoB(str) 
        EchoB = Response.Write(str & "<br>" & vbCrLf) 
    End Function 
    
    Sub Cleanup(obj) 
        IF isObject(obj) Then 
            Set obj = Nothing 
        End IF 
    End Sub 
    
    Function StripFileName(strFile) 
        StripFileName = Left(strFile, inStrRev(strFile, "\")) 
    End Function 
    
    Sub BuildTableCol(strData) 
        Echo("<td class=DataCol>" & strData & "</td>") 
    End Sub 
    
    %>
    Can anyone help me sort this out?

    Thanks very much/...

  5. #5
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    The only changes i made were in the second For Each loop where you were displaying the images.

    All i did was added a <table> tag (which you might need to remove. you may have your page set up differently.
    I also pulled the <tr> tag to the outside of the loops. If you left them in the loop then on each loop it was creating a new row.

    Then i added this if statement.

    If x Mod 5 Then
    'do nothing, we just need to display the image.
    Else
    Echo("</tr><tr>")
    end if

    Which basically says if x Mod 5 then Dispaly the image,
    Code:
    Sub GetAllFiles() 
        Dim oFS, oFolder, oFile 
        Set oFS = Server.CreateObject("Scripting.FileSystemObject") 
             
        'Set Folder Object To Proper File Directory 
        Set oFolder = oFS.getFolder(FILE_FOLDER) 
         
        Dim intCounter 
         
        intCounter = 0 
         
            Dim FileArray() 
            ReDim Preserve FileArray(oFolder.Files.Count, 1) 
         
            For Each oFile in oFolder.Files 
                strFileName = oFile.Name 
                     
                FileArray(intCounter, 0) = strFileName 
                FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>" 
             
                intCounter = (intCounter + 1) 
            Next 
             
            intRows = uBound(FileArray, 1) 
            intCols = uBound(FileArray, 2) 
         
    		Echo("<table>")
    		Echo("<tr>") 
            For x = 0 To intRows -1 
                For z = 0 To intCols 
                    If z > 0 Then 
    					If x MOD 5 Then 
    						'Do nothing
    					else
    						Echo("</tr><tr>")
    					end if 
    					BuildTableCol(FileArray(x, z)) 
                    End If 
                Next 
            Next 
            Echo("</tr>") 
            Echo("</table>")
        EchoB("<B>" & oFolder.Files.Count & " Files Available</B>") 
             
        Cleanup oFile 
        Cleanup oFolder 
        Cleanup oFS 
    End Sub

  6. #6
    Join Date
    May 2003
    Location
    UK
    Posts
    220
    Magic!!

    Don't suppose you know how I would filter only files with certain extentions (.jpg, .jpeg, .gif, etc)?

    Also, for some reason, if I change;

    Code:
    FILE_FOLDER = StripFileName(Request.ServerVariables("PATH_TRANSLATED"))
    to;

    Code:
    FILE_FOLDER = "/images/pfi/thumb/"
    I get a path not found error message!

    Thanks/...
    Last edited by ldoodle; 02-09-2006 at 11:11 AM.

  7. #7
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    You could do it multiple ways. You just need to check to see what the last 3 characters are. You could Split the strFileName @ the . and then use the upper bound of that array or you could just do a right like this.

    If Right(strFileName, 3) = "JPG" then
    FileArray(intCounter, 0) = strFileName
    FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>"
    End if

    NOW by doing this it will throw your count off. Your loop that displays the records is going to loop to intRows which is set to the total number of files in that directory. You will need to come up with a way. With out trying it what might work is to do this.
    Code:
    If Right(strFileName, 3) = "JPG" then 
          FileArray(intCounter, 0) = strFileName 
          FileArray(intCounter, 1) = "<a href=" & strFileName & "><img src=" & strFileName & "></a>" 
           intCounter = (intCounter + 1)  'place the counter in here that way you only count the records that matched your criteria.
    End if 
    
    'Then isstead of using  intRows = uBound(FileArray, 1)  set your Rows to the counter.
    intRows = intCounter 
    intCols = uBound(FileArray, 2)
    This has not been tested. See if that helps.

  8. #8
    Join Date
    May 2003
    Location
    UK
    Posts
    220
    Even more magic!!

    Last 2 problem... if you go to the page in question: http://www.aylesford.kent.sch.uk/ima...agegallery.asp

    You'll see that it's put an extra row just above the first row of pictures. I've tried moving the TR's about, with no luck.

    Also, can I specify a message to be displayed if no images are found in the directory?

    Any suggestions?

    Thanks again/...
    Last edited by ldoodle; 02-09-2006 at 11:51 AM.

  9. #9
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    Try this
    For x = 1 To intRows -1

    Instead of
    For x = 0 to IntRows - 1

    Your getting the blank because when x = 0 Then x MOD 5 is not true and thus it ends the current row and starts a new row.

  10. #10
    Join Date
    May 2003
    Location
    UK
    Posts
    220
    Nope, that didn't work. If it's set to 1, it removes the first image. If it's set to 2, it removes the first 2 images. It carries on like this.

    Getting so close now!!

    Any other suggestions?

    Thanks again/...

  11. #11
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    Well an easy fix is this. This is tested and it works.
    But i dont necessarily like it. But for a quick kill it will work.
    The problem you are having is that when x = 0 then
    x MOD 5 is not true so it drops in the else statement and ends the </tr> tag and starts a new one. Try this is should work.

    If x > 0 then
    If x MOD 5 Then
    Else
    Echo("</tr><tr>")
    End If
    End if
    BuildTableCol(FileArray(x,z))

  12. #12
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    or this. same thing just looks better
    Code:
    If x MOD 5 Then 
    else
         if x > 0 then 
    	Echo("</tr><tr>")
         Else
    end if

  13. #13
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    or what solves all of it is to set intcounter = 1 so that your array stats at 1 instead of 0 then you could just do

    For x = 1 to intCounter.

    Any of those way should work.

    EDIT:
    actually this will not work. It causes other problems.
    Last edited by lmf232s; 02-09-2006 at 03:30 PM.

  14. #14
    Join Date
    May 2003
    Location
    UK
    Posts
    220
    The second solution works perfectly.

    One very last question... on the filtering of file extensions, i'm using

    Code:
    If Right(strFileName, 3) = "bmp" or Right(strFileName, 3) = "gif" or Right(strFileName, 3) = "jpg" or Right(strFileName, 3) = "jpeg" or Right(strFileName, 3) = "png" or Right(strFileName, 3) = "tiff" Then
    to pick up all 6 file types.

    Is there a neater way of doing this? I've tried "bmp, gif" and "bmp", "gif" and "bmp" or "gif", all of which don't work.

    Thanks for all your help/...

  15. #15
    Join Date
    Jun 2004
    Location
    Kansas City, MO
    Posts
    1,607
    Yes try this
    Delcare this array at the top of your page
    Code:
    Dim arrExt : arrExt = Array("jpg", "gif", "bmp", "jpeg", "png", "tiff")
    Then change this line
    Code:
    If Right(strFileName, 3) = "?" Then 
    To 
    If StripMe(Right(strFileName, 3), arrExt) then
    Then add this function
    Code:
    Function StripMe(str, arrExt)
    	Dim i 
    
                 For i = 0 To UBound(arrExt)
    		If str = arrExt(i) Then 
    			StripMe = True
    		End if 
    	Next
    End Function
    Change the names as you feel fit. This should work. I tested it on my end and it seemed to work but you know how that goes.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles