user2034843
user2034843

Reputation: 11

How can I perform checks on each file that is to be uploaded?

I am trying to make a multiple upload system for images. I am using input type of file with multiple files upload. I want to have checks for each and every file and upload only if they pass all the checks, otherwise stop uploading the current (failing) file but continue for remaining files. I want to do it with Javascript and PHP. Can anyone suggest what steps to follow? No JQuery, please.

    <input id="uploadfiles"  style="overflow:hidden" type="file"  name="files[]" value="upload" multiple />

Upvotes: 0

Views: 82

Answers (1)

Kyborek
Kyborek

Reputation: 1511

Let me summarize generic upload process:

  1. User will choose file(s)
  2. They will click upload (or anything that submits the form)
  3. File will get uploaded using multipart/form-data form to php temporary folder
  4. Php script will be called (target of the form) and it will be passed file paths in the tmp folder
  5. Php script will decide to copy files from tmp folder to some folder on the web page

This clearly shows one thing: If you want to validate files by PHP script, all of the files must have been fully uploaded already, this means you can not call the script for each file separately while others are uploading (not with html and regular forms).

Also (depending on requirements you need to check on files) you may not be able to validate files with javascript for some security reasons.

So there is simple (but not much satisfying) solution. Let the user upload all files, then validate them one by one with your javascript. If they pass your requirements, copy them to intended folder, if not you can just leave them intact and they will be erased from the tmp folder. It has only one downside, the user will need to wait long time when uploading large file before they know it did not pass requirements.

Upvotes: 1

Related Questions