ASP File Object Issue

405 views Asked by At

I am working on an ASP classic website, which the client reported suddenly exhibited an issue with a previously working function which listed only "image" file types. Upon reading the code, I found that the loop that lists the files in a folder, uses the InStr() function to identify files by their type, which should be "image." However, I found that something must have changed in the OS, as the type is not longer "image", but "JPG", or "PNG", etc. This dramatically changes the way the code works. Following is a snipped of the code:


Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath(sCurrentDirectoryPath))
Set oSubFolder = oFolder.Files
                            
    iFileCount = 0
                            
    For Each oFileName in oSubFolder
                
        If InStr(1, LCase(oFileName.Type),"image") > 0 Then
            iFileCount = iFileCount + 1
        End If
    Next

Because the InStr() function is trying to find a file type of "image", no files are counted up, and the function returns zero files found. Whilst debugging, I found that the value being returned by oFileName.Type, was as follows:

This is the file type:JPG File This is the file type:JPG File This is the file type:Text Document This is the file type:Data Base File

Files in the folder were two "whatever.jpg" files, a "whatever.txt" file, and a "thumbs.db" file. So, it appears that the OS (Windows Server 2019) may have changed to be less generic with reporting an "image" file, and is now reporting "JPG file" or "PNG file", etc. This of course, breaks this code! Are there any suggestions from you'all on how I could go about modifying this code to work on reporting exactly how many image files are present?

1

There are 1 answers

3
Adam On BEST ANSWER

On Windows 10, the Type values for .jpg and .png files are JPEG image and PNG image respectively. What OS are you using?

Also, Type doesn't actually analyze the file, you could have a virus.exe file in the folder that has been renamed to virus.jpg, and the Type value will still show it as JPEG. So if the function is indented to check user uploaded content to ensure images are actually images, the Type value will be of no use. If you have root access you could install a COM DLL that uses a program such as ExifTool to properly analyze files (https://github.com/as08/ClassicASP.ExifTool), however that would be a complete rewrite.

But assuming you're not looking to check if an image file is actually an image file, you could just split the filename extensions and use Select Case to count the image files if your OS is returning just XXX file and no longer XXX image in the Type value (alternatively you could split the Type value, but you'd still need to check for valid image file extensions):

Dim oFSO, oFolder, oSubFolder, oFileName, iFileCount, oFileNameSplit

Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath(sCurrentDirectoryPath))
Set oSubFolder = oFolder.Files
                        
    iFileCount = 0
                        
    For Each oFileName In oSubFolder
    
        oFileNameSplit = Split(oFileName.Name,".")
        
        If uBound(oFileNameSplit) > 0 Then
        
            Select Case Trim(lCase(oFileNameSplit(uBound(oFileNameSplit))))
            
                Case "jpg", "jpeg", "png", "gif" ' Maybe add some more extensions...
                
                iFileCount = iFileCount + 1
                
            End Select
        
        End If
    
    Next

Set oFSO = Nothing
Set oFolder = Nothing
Set oSubFolder = Nothing

Response.Write(iFileCount)