0

I'm working with some code that uploads an image from a form and stores it on our server. In Internet Explorer the user can enter a path manually, and I wonder how I can check that the file exists, i.e., that the user entered a valid path.

There's a FileItem object that's being used to check size (e.g., fileItem.getSize() < MAX_SIZE), and I wonder if a good approach would be to use size to check that the file exists. For example:

if (fileItem.getSize() == 0) {
  // Somethings wrong -- invalid path.
} else {
  // File exists -- valid path.
}

Any suggestions are appreciated. Thanks!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Chocula
  • 1,946
  • 1
  • 16
  • 21
  • I think you don't even get a FileItem when there is nothing uploaded. – akarnokd Jun 26 '09 at 18:01
  • I do get a FileItem, but the size is zero for any garbage path entered. – Chocula Jun 26 '09 at 18:03
  • Thank you, wasn't sure about that as I never tested my file uploads by not uploading something. I guess I you know what type of things are allowed to be uploaded and don't really expect a zero-length file you could just stay with your method. – akarnokd Jun 26 '09 at 18:14

1 Answers1

1

On the client, you cannot reliably read the text of a file upload control with script. IE8 and Opera10, for instance, will lie to you and provide a generic path containing "C:\fakepath\". This is done for privacy reasons.

On the server, you can do exactly as you've done, simply check to see that you actually got a file in the upload, and if so, then you can examine the file, determine if it matches your criteria.

EricLaw
  • 56,563
  • 7
  • 151
  • 196