File Upload Web Form

This section provides a tutorial example on how to write Web form to upload files. type='file' attribute of the 'input' displays 'Choose File' button for user to select a local file to upload.

To manage file uploads with PHP scripts, you need to build a Web form for users to file, select and submit files from their local computers. Here are some guidelines on building file upload forms:

1. Using <form method="POST" ...> to ensure that uploaded files are included in the HTTP request body.

2. Using <form enctype="multipart/form-data" ...> to ensure that uploaded files are properly encoded as multiple attachments.

3. Using <input type="hidden" name="MAX_FILE_SIZE" value="nnnnn"/> before any <input type="file" .../> elements to limit the upload file size to the given number of bytes.

4. Using <input type="file" ...> to display a "Choose File" button to allow the user to search and select a local file to upload.

Here is an example of a Web form, file-upload-form.html, for file upload.

<html>
<!-- file-upload-form.html
- Copyright (c) HerongYang.com. All Rights Reserved.
-->
<body>
  <h4>File Upload Form</h4>
  <form action="file-upload-handler.php" method="POST"
    enctype="multipart/form-data">
    <p>1st file to upload: <input type="file" name="file_one"/></p>
    <p>2nd file to upload: <input type="file" name="file_two"/></p>
    <p>Click to upload: <input type="submit" name="submit" value=Upload></p>
  </form>
</body>
</html>

Put the Web form file, file-upload-form.html, to the Apache Web server on the local computer. Then open this form with a Web browser using http://localhost/file-upload-form.html.

Click "Choose File" buttons to select two different files, Hello.php and dot.gif:

Web Form for File Upload
Web Form for File Upload

See the next tutorial on how to write file-upload-handler.php to received the uploaded files on the Web server.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

Managing File Upload

File Upload Web Form

 File Upload PHP Script

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

 Configuring and Sending out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Outdated Tutorials

 References

 Full Version in PDF/EPUB