What is the php file upload process?

coldplay.xixi
Release: 2023-03-08 09:50:01
Original
3015 people have browsed it

php file upload process: first configure the PHP file and set the front-end form; then save the temporary file in the set directory by default; then judge and process the error attribute in the file array; finally, in the front-end you can Limit the type and size of uploaded files.

What is the php file upload process?

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer. This method is suitable for all brands of computers.

php file upload process:

1. PHP configuration file

The file uploads section in the php.ini file defines the relevant file upload configuration.

  • file_uploads = On Turn on file uploads

  • upload_tmp_dir =Temporary directory

  • upload_max_filesize = 2M maximum Upload size

  • max_file_uploads = 20 Maximum number of files uploaded at a time

2. Front-end form

Copy after login

When using the file When uploading the form of the control, the enctype of the form must be set to "multipart/form-data".

You can limit the size and type of uploaded files on the client side:

 
Copy after login

3. Background processing

After uploading, you will get a$_FILES on the server sideThe file array is as follows:

What is the php file upload process?

The temporary files uploaded to the server are saved by default in the directory set byupload_tmp_dirin php.ini, we need Move the file to the specified location:


        
Copy after login

4. Error handling

Judge and process the error attribute in the file array:

$error=$myfile['myfile']['error']; if ($error==0){//为0,代表上传成功 if (move_uploaded_file($tmp,"userpic/".$picname)) { echo "移动文件成功"; }else{ echo "移动文件失败"; } }else{//不为0,匹配错误信息 switch ($error){ case 1: echo "超过文件最大上传限制";//php.ini中upload_max_filesize break; case 2: echo "超过表单文件大小限制";//HTML表单中MAX_FILE_SIZE选项 break; case 3: echo "文件部分被上传"; break; case 4: echo "未选择上传文件"; break; case 7://文件写入失败 case 8: echo "系统错误"; break; } }
Copy after login

5. Upload restrictions

On the front end, you can limit the type and size of uploaded files:

   
Copy after login

Note that the limit is in bytes

There are also response restrictions on the server side:

$size=$myfile['size'];//获取文件大小 $MaxSize=2097152;//设置最大允许字节 $type=$myfile['type'];//获取图片类型 $AllowTypr=array('image/jpeg','image/png','image/gif');//允许类型数组 if ($error==0){//为0,代表上传成功 if ($size>$MaxSize){ echo ""; echo ""; exit(); } if (!in_array($type,$AllowTypr)){ echo ""; echo ""; exit(); }
Copy after login

Related video recommendations:PHP programming from entry to proficiency

The above is the detailed content of What is the php file upload process?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!