Table of Contents
PHP file upload form ~~Study Notes
Home Backend Development PHP Tutorial PHP file upload form ~~Study notes_PHP tutorial

PHP file upload form ~~Study notes_PHP tutorial

Jul 13, 2016 am 10:02 AM
document notes form

PHP file upload form ~~Study Notes

PHP file upload
Through PHP, files can be uploaded to the server.
-------------------------------------------------- -------------------------------------------------- ---------------
Create a file upload form: useful when allowing users to upload files from a form;

Below is an html form for uploading files:

1

2

3

4

5

6

7

<html>

<body>

 

 

 

</body>

</html>

Copy after login

The enctype attribute of the
tag specifies which content type to use when submitting the form. When a form requires binary data, such as file content, use "multipart/form-data".
The type="file" attribute of the tag specifies that the input should be processed as a file. For example, when previewing in a browser, you'll see a browse button next to the input box.

Note: Allowing users to upload files is a huge security risk. Only allow trusted users to perform operations on files.
-------------------------------------------------- -------------------------------------------------- ---------------
Create upload script:
The "upload_file.php" file contains the code for uploading files:

1

2

3

4

5

6

7

8

9

10

<?php

<span style="white-space:pre">    </span>if($_FILES["file"]["error"] > 0) {

<span style="white-space:pre">        </span>echo "Upload Error: ". $_FILES["file"]["error"] . "<br />";

<span style="white-space:pre">    </span>}else {

<span style="white-space:pre">        </span>echo "Upload : ". $_FILES["file"]["name"] . "<br />";

<span style="white-space:pre">        </span>echo "Type : ". $_FILES["file"]["type"] . "<br />";

<span style="white-space:pre">        </span>echo "Size : ". $_FILES["file"]["size"]/1024 . " kb<br />";

<span style="white-space:pre">        </span>echo "Store in : ". $_FILES["file"]["tmp_name"] . "<br />" ;

<span style="white-space:pre">    </span>}

?>

Copy after login

By using PHP's global array $_FILES, you can upload files from the client computer to a remote server.

The first parameter is the input name of the form, the second parameter can be: "name", "type", "size", "tmp_name" or "error". just like this:
$_FILES["file"]["name"] - the name of the uploaded file
$_FILES["file"]["type"] - the type of file being uploaded
$_FILES["file"]["size"] - Size of the uploaded file, in bytes
$_FILES["file"]["tmp_name"] - The name of the temporary copy of the file stored on the server
$_FILES["file"]["error"] - error code caused by file upload

This is a very simple way to upload files. For security reasons, you should add restrictions on who has permission to upload files.

In this script, we add restrictions on file uploads. Users can only upload .gif or .jpeg files, and the file size must be less than 20 kb:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php

 

<span style="white-space:pre">    </span>if ((($_FILES["file"]["type"] == "image/gif")

<span style="white-space:pre">    </span>|| ($_FILES["file"]["type"] == "image/jpeg")

<span style="white-space:pre">    </span>|| ($_FILES["file"]["type"] == "image/pjpeg"))

<span style="white-space:pre">    </span>&amp;&amp; ($_FILES["file"]["size"] < 20000)) {

<span style="white-space:pre">        </span>if ($_FILES["file"]["error"] > 0) {

<span style="white-space:pre">            </span>echo "Error: " . $_FILES["file"]["error"] . "<br />";

<span style="white-space:pre">        </span>}else {

<span style="white-space:pre">            </span>echo "Upload: " . $_FILES["file"]["name"] . "<br />";

<span style="white-space:pre">            </span>echo "Type: " . $_FILES["file"]["type"] . "<br />";

<span style="white-space:pre">            </span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

<span style="white-space:pre">            </span>echo "Stored in: " . $_FILES["file"]["tmp_name"];

<span style="white-space:pre">        </span>}

<span style="white-space:pre">    </span>}else {

<span style="white-space:pre">        </span>echo "Invalid file";

<span style="white-space:pre">    </span>}

 

?>

Copy after login

Note: For IE, the type to recognize jpg files must be pjpeg, and for FireFox, it must be jpeg.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<?php

<span style="white-space:pre">    </span>if ((($_FILES["file"]["type"] == "image/gif")

<span style="white-space:pre">    </span>|| ($_FILES["file"]["type"] == "image/jpeg")

<span style="white-space:pre">    </span>|| ($_FILES["file"]["type"] == "image/pjpeg"))

<span style="white-space:pre">    </span>&amp;&amp; ($_FILES["file"]["size"] < 20000)) {

<span style="white-space:pre">        </span>if ($_FILES["file"]["error"] > 0) {

<span style="white-space:pre">            </span>echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

<span style="white-space:pre">        </span>}else {

<span style="white-space:pre">            </span>echo "Upload: " . $_FILES["file"]["name"] . "<br />";

<span style="white-space:pre">            </span>echo "Type: " . $_FILES["file"]["type"] . "<br />";

<span style="white-space:pre">            </span>echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

<span style="white-space:pre">            </span>echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

 

<span style="white-space:pre">            </span>if (file_exists("upload/" . $_FILES["file"]["name"])) {

<span style="white-space:pre">                </span>echo $_FILES["file"]["name"] . " already exists. ";

<span style="white-space:pre">            </span>}else {

<span style="white-space:pre">                </span>move_uploaded_file($_FILES["file"]["tmp_name"],

<span style="white-space:pre">                </span>"upload/" . $_FILES["file"]["name"]);

<span style="white-space:pre">                </span>echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

<span style="white-space:pre">            </span>}

<span style="white-space:pre">        </span>}

<span style="white-space:pre">    </span>}else {

<span style="white-space:pre">        </span>echo "Invalid file";

<span style="white-space:pre">    </span>}

?>

Copy after login

The above script detects whether the file already exists, and if it does not exist, copies the file to the specified folder.

Note: This example saves the file to a new folder named "upload".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/969604.htmlTechArticlePHP file upload form~~Study notes PHP file upload Through PHP, files can be uploaded to the server. -------------------------------------------------- --------------------------...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to recover expired WeChat files? Can expired WeChat files be recovered? How to recover expired WeChat files? Can expired WeChat files be recovered? Feb 22, 2024 pm 02:46 PM

How to recover expired WeChat files? Can expired WeChat files be recovered?

Photos cannot open this file because the format is not supported or the file is corrupted Photos cannot open this file because the format is not supported or the file is corrupted Feb 22, 2024 am 09:49 AM

Photos cannot open this file because the format is not supported or the file is corrupted

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes

Can Tmp format files be deleted? Can Tmp format files be deleted? Feb 24, 2024 pm 04:33 PM

Can Tmp format files be deleted?

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? Mar 14, 2024 pm 02:07 PM

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk?

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code.

What is hiberfil.sys file? Can hiberfil.sys be deleted? What is hiberfil.sys file? Can hiberfil.sys be deleted? Mar 15, 2024 am 09:49 AM

What is hiberfil.sys file? Can hiberfil.sys be deleted?

How to install GHO files How to install GHO files Feb 19, 2024 pm 10:06 PM

How to install GHO files

See all articles