Core PHP Programming - File Upload

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/yangmolulu/article/details/91467338

File Upload

table of Contents

File Upload

principle

Written form

$ _FILES variable Detailed

Move the temporary files to the target location

Multiple file uploads

Data structure in the form of multi-file upload $ _FILES variable

Traversal of the multi-file read and process information

Upload file follow-up questions


principle

File uploads: files from the user's local computer saved to the server computer where the directory specified by the transmission (web forms).

 

1, increasing the file upload form: a server browser requests an HTML script (include file upload form)

2, the user selects a file from the local (click the upload box (button))

3, the user clicks the Upload button: the file will be transferred to the server via the Internet,

4, server operating system will save the file to a temporary directory: Temporary files are saved in a format (Windows under tmp)

5, the server script to work: to determine the file is valid

6, the server script will effectively move the file from the temporary directory to the specified directory (complete)

 

 

 

Written form

1) method attribute: a file upload form submission must be POST

2) entype attributes: form Form Properties, mainly standardized form of data encoding.

 

3) upload form: file Form

 

 

 

$ _FILES variable Detailed

In PHP, there is a predefined variable $ _FILES is designed to store the user to upload the file.

 

 

Test: If there is no enctype attribute, upload files look like?

 

 

1) name: files on the user (browser) computer name actually exists (to retain the actual suffix)

2) tmp_name: upload files to a temporary path to the server operating system saved (PHP actually used to later use)

3) type: MIME type (Multipurpose Internet Mail Extensions), is used in the computer client identify the file type (determined software)

4) error: file upload code that tells the application software (PHP) files what's wrong reception process (post-judgment according to the code PHP files)

 

5) size: file size (PHP late to determine whether or not according to the actual needs of the reservation)

Move the temporary files to the target location

After the saved file is uploaded to the $ _FILES, then the information is in the form of access to the file $ _FILES [ 'form name attribute value'] [ 'element information']

 

1)判断是否为上传文件:is_uploaded_file()

2)移动文件:move_uploaded_file(),也返回布尔值。如果移动成功,文件就被保存到指定目录下。

 

出现问题:文件权限。

 

修改c:\windows\temp的访问权限,赋予IUSR_用户可写

 

 

多文件上传

当商品需要上传多个图片进行展示的时候:那么需要使用多文件上传

       针对一个内容但是不同文件说明:同名表单

 

当商品需要进行多个维度图片说明的时候:需要使用多个文件上传

       针对是不同内容所以表单名字不一样:批量解决问题

 

多文件上传的$_FILES变量的数据结构形式

 

批量上传:

1、同名表单,将表单名字形成一个数组,而且同时将文件对应的五个要素:name,tmp_name, size ,type, error都形成对应的数量的数组,每个文件上传对应数组元素的下标都是一样的:name[0]和type[0]是属于同一个文件

 

2、不同名表单,每个文件都回形成一个属于自己独立的5个元素的数组

 

对多文件信息的遍历读取和处理

 

1、不同名多文件上传处理方式:按照表单名字从$_FILES中取出来就可以直接使用(明确知道表单中有多少个文件上传);如果不确定表单中有多少个文件上传,通过遍历$_FILES数组,挨个取出来实现文件上传。

 

2、同名多文件上传:想办法得到一个文件对应的五个元素数组。从$_FILES中把对应的name\tmp_name\size\error\type挨个取出来,然后存放到不同的数组中。

 

文件上传后续问题

实现上传功能代码的重复利用:封装文件上传函数

功能:上传文件

条件:条件判断

              需要上传的文件信息:对应的5个元素数组

              1、文件类型是否合适?外部指定MIME类型

              2、文件存储到什么位置?外部指定

              3、文件格式限制(文件后缀)?外部限定

              4、文件大小限制?外部指定

结果:实现文件上传

       1、成功:结果能够在以后看到:需要将文件的路径和文件名字返回

       2、失败:返回false,指定错误原因(引用参数)

 

 

1)封装除一个上传函数

 

2)判断文件是否有效

 

3)判断保存路径是否有效

 

4)判断文件本身上传的过程中是否有错误

 

5)文件类型的处理:通过MIME匹配即可

 

6)文件格式的处理:后缀名的问题

 

 

7)文件大小的处理

 

8)移动到指定目录

 

9)命名冲突的处理:上传同名文件?中文名字文件怎么办?

 

Guess you like

Origin blog.csdn.net/yangmolulu/article/details/91467338