Solved

check file ext

Posted on 2006-11-17
4
247 Views
Last Modified: 2013-12-24
hi i need to check my file ext before i upload, at the moment it does not work??


            <!---check its a image--->            
            <cfset allowfiles = "jpg,jpeg,gif,png">      
            <cfif listfindnocase(allowfiles,'form.image')>
            <cfset error = "Error: An articsdvfdst">
            </cfif>
0
Comment
Question by:pigmentarts
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
dgrafx earned 250 total points
ID: 17964816
can't do it that way
<script>
function fileType(field) {
var ok = true;
var Extensions = ".jpg,.gif";      
      
if(field!=null && field.value.length) {
      dots = field.value.split(".")
      thisType = "." + dots[dots.length-1]
      thisType = thisType.toLowerCase()            
      if(Extensions.toLowerCase().indexOf(thisType) == "-1") {
            ok = false;                        
      }
}      
      if(!ok) {
      alert("File Type is Not Allowed.\nUse "+Extensions+" Only.")
      return false
      }
      
}
</script>
<input type="file" name="imgfile" onblur="fileType(this)">

but if user doesn't have js enabled then you need to use CF
<cfif Not listfindnocase(Extensions,cffile.serverFileExt)>
File is not allowed :)
</cfif>
you'd do this after your cffile action="upload"
0
 
LVL 20

Assisted Solution

by:trailblazzyr55
trailblazzyr55 earned 250 total points
ID: 17965183
you could check using the ListFindNoCase() method however in your method you have to specify the delimeter as ".", default is a "," so this would be more correct...

<cfif not listFindNoCase(extList,cffile.serverFileExt,".")>
   Not Allowed
</cfif>

however, you can also filter/allow certain files to be uploaded in the cffile tag as well..., and therefore you wouldn't have to do this check above...
well you could to display a message to the user or something...

<cffile
  action = "upload"
  fileField = "form.field"
  destination = "you_absolute_path"
  nameConflict = "MakeUnique"
  accept = "image/jpg, image/jpg, image/jpeg, image/gif, image/png">
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17965202
please ignore the duplicate image/jpg in the accept attib... this is more correct

<cffile
  action = "upload"
  fileField = "form.field"
  destination = "you_absolute_path"
  nameConflict = "MakeUnique"
  accept = "image/jpg, image/jpeg, image/gif, image/png">
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 17965517
thanks,

used both


doe the cf check first then just in case done,

 accept = "image/jpg, image/jpeg, image/gif, image/png">
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question