Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating files

Status
Not open for further replies.

FSonaglioni

Programmer
Jul 11, 2000
13
AR
Hi , how can i validate if a user attaches a valid type of&nbsp;&nbsp;file using an &lt;input type=file?&gt; , cause if i submit the form&nbsp;&nbsp;and that field is empty&nbsp;&nbsp;it returns me a message error when triying to upload the file! <br><br>and then using a &lt;cffile&gt; how could i rename this file (because using cfffile with the option &quot;MAKEUNIQUE&quot; )generates a unique filename for the upload .<br><br>Thanks in advance!!
 
You can set the ACCEPT parameter to the mime type of the type of file you want, like<br><FONT FACE=monospace><b><br>&lt;CFFILE ACTION=&quot;Upload&quot; FILEFIELD=&quot;TheFile&quot; DESTINATION=&quot;c:\web\uploads\&quot; ACCEPT=&quot;text/html&quot; NAMECONFLICT=&quot;MakeUnique&quot;&gt;<br></b></font><br>If you do not want to use the ACCEPT parameter (you just want to check an extension, you could do something like:<br><FONT FACE=monospace><b><br>&lt;CFFILE ACTION=&quot;Upload&quot; FILEFIELD=&quot;TheFile&quot; DESTINATION=&quot;c:\web\uploads\&quot; NAMECONFLICT=&quot;MakeUnique&quot;&gt;<br><br>&lt;cfif file.ClientFileExt is &quot;htm&quot; or file.ClientFileExt is &quot;html&quot;&gt;<br><br>&nbsp;&nbsp;&lt;cfset filename=file.ServerFile&gt;<br>&nbsp;&nbsp;&lt;cfset directoryname=file.ServerDirectory&gt;<br><br>&nbsp;&nbsp;&lt;cfif file.FileWasRenamed&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfset cfilename=file.ClientFileName&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfset cfileext=file.ClientFileExt&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;cffile action=&quot;rename&quot; source=&quot;#directoryname#\#filename#&quot; destination=&quot;#directoryname#\#cfilename#1.#cfileext#&quot;&gt;<br>&nbsp;&nbsp;&lt;/cfif&gt;<br><br>&lt;cfelse&gt;<br><br>&nbsp;&nbsp;&lt;cffile action=&quot;delete&quot; file=&quot;#directoryname#\#filename#&quot;&gt;<br><br>&lt;/cfif&gt;<br></b></font><br>There is one problem with this code in that if you upload a file called file.htm and it already exists, it will be renamed file1.htm whether or not file1.htm already exists.&nbsp;&nbsp;You should probably do a more thorough check for this condition....<br><br>Also, I can't remember off the top of my head whether file.serverdirectory returns the path with a trailing backslash or not.&nbsp;&nbsp;If it does, you should take the backslashes out...<br><br>Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top