如何要求用户上传文件 - php?

I'm trying to create a large, multi-page, PHP web application for work where a client enters information and receives output (think tax software). However, I can't seem to figure out how to require that a file is uploaded by the client. By files, I mean mostly PDF, txt, JPEG, etc.

There are several file upload fields that must be uploaded for the output to contain all of the necessary information to work, and several other file upload fields that are optional.

I would like to do this in PHP if possible, since as I understand it, Javascript could be turned off and that would stop the validation.

On the PHP file that processes the form you'd want to use something like this:

if (isset ($_FILES['myfile']['tmp_name']) && !empty ($_FILES['myfile']['tmp_name'])) {
  // process file upload
} else {
  echo 'You must upload a file in the "myfile" field to proceed!';
}

This would be in addition to other proper form validation techniques of course.

First of all, you shouldn't validate your forms with JavaScript only, a server-side validation should always be done.

Regarding your question, when you upload a file with PHP, you have its information in the $_FILES array. You will need to look there to know if a file has been uploaded or if it is in the right format, etc.

Validation must be done server side. To upload a file, use html <input type="file" name="example">

This will upload the file information to an array $_FILES['example']

You should checkout this Upload Tutorial

if(!isset($_FILES["fileinputname"][]))
{ 
die("Upload files, you must.");
}

You should check out this article

also, for html validation, you can use this.

(Most people don't like it, it's personal preference for many people)

may work, but html validation can be bypassed. IE and Safari don't support it.

http://www.w3schools.com/html5/att_input_required.asp