Question

I ran into this problem when uploading a file with a super long name - my database field was only set to 50 characters. Since then, I have increased my database field length, but I'd like to have a way to check the length of the filename before uploading. Below is my code. The validation returns '85' as the character length. And it returns the same count for every different file I upload (none of which have a file name length of 85).

    <cfscript>
    missing_info = "<p>There was a slight problem with your submission. The following are required or invalid:</p><ul>";
    // Check the length of the file name for our database field
    if ( len(Form["ResumeFile1"]) gt 100 )
    {
    missing_info = missing_info & "<li>'Resume File 1' is invalid. Character length must be less than 100. Current count is " & len(Form["ResumeFile1"]) & ".</li>";
    validation_error = true;
    ResumeFileInvalidMarker  = true;
    }
    </cfscript>

Anyone see anything wrong with this?

Thanks!

Was it helpful?

Solution

A quick clarification in the wording of your question. By the time your code executes the file upload has already happened. The file resides in a temporary directory on the ColdFusion server and the form field related to the file upload contains the temporary filename for that file. Aside from checking to see if a file has been specified, do not do anything directly with that file or you'll be circumventing some built in security.

You want to use the cffile tag with the upload action (or equivalent udf) to move the temp file into a folder of your choosing. At that point you get access to a structure containing lots of information. Usually I "upload" into a temporary directory for the application, which should be outside of the webroot for security.

At this point you'll then want to do any validation against the file, such as filename length, file type, file size, etc and delete the file if it fails any checks. If it passes all checks then you move it into it's final destination which may be inside the webroot.

In your case you'll want to check the cffile structure element clientFile which is the original filename including extension (which you'll need to check, since an extension doesn't need to be present and can be any length).

OTHER TIPS

http://www.cfquickdocs.com/cf9/#cffile.upload

After you upload the file, the variable "clientFileName" will give you the name of the uploaded file, without a file extension.

The only way to read the filename before you upload it would be to use JavaScript to read and parse the value (file path) in the file field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top