How to implement file upload in PHP
1. Form part
Allow users to upload files, add an upload attribute to the declaration of the HTML form:
enctype = 'multipart/form-data'
The method of the form must be POST
The form option MAX_FILE_SIZE hidden field is used to limit the size of the uploaded file , it must be placed in front of the file form element, and the unit is bytes.
The code is as follows:
<form enctype='multipart/form-data' id='aa' name='aaa' method='post' action='xxx.php'> <input type='hidden' name='MAX_FILE_SIZE' value='2621114' /> <input name='upload_file' type='file' /> </form>
Related recommendations: "PHP Getting Started Tutorial"
2. Processing uploaded files
When uploading, PHP receives an array of information about the file, which can be found in the super global array $_FILES.
For example: If the name of the file input box in the form is upload_file, then all information about the file is included in the array $_FILES['upload_file'].
For example: The customer uploaded an image array value of "aaa.jpg" as follows:
name "p5pp.jpg" The name of the file when uploading
type "image/ jpeg" file type
tmp_name "/tmp/phpjksdf" The temporary file name on the server side
error The return value of upload error
size 2045 The actual size of the file
The errors in the above array will return different constant values, as follows:
UPLOAD_ERR_OK No error occurred, the file upload was successful
UPLOAD_ERR_INI_SIZE The file size exceeds the value limited by the upload_max_filesize option in PHP.INI
UPLOAD_ERR_FORM_SIZE The size of the uploaded file exceeds the MAX_FILE_SIZE option value in the HTML form. You can check the form $FILES ['up_file']['size'] in the program to handle
UPLOAD_ERR_PARTIAL Only part of the file was uploaded
UPLOAD_ERR_NO_FILE The user did not provide any file upload
Specific example of post-upload processing:
The code is as follows:
if(!move_uploaded_file($_FILES['f']['tmp_name'],"uploads/".$_FILES['f']['name'].".jpg")){ echo "error"; }
Function:
move_uploaded_file moves the uploaded temporary file to the specified directory
Example:
move_uploaded_file('temporary file name','specified file path')
is_uploaded_file determines whether it is a file uploaded through http Post
Example:
The code is as follows:
if(!is_uploaded_file($_FILES['f']['tmp_name'])){ echo '非法'; }
3. Related parameters
Parameters in php.ini that PHP uploads the design to:
file_uploads Whether to allow file uploads, default ON.
upload_tmp_dir Temporary directory for uploading files. If not specified, the system default location will be used.
upload_max_filesize The maximum size of allowed uploaded files, the default is 2M.
post_max_size controls the maximum amount of data that PHP can accept in a form submission using the POST method. If you want to use PHP files to upload, this value should be changed to be larger than upload_max_filesize.
max_input_time limits the time in seconds for receiving data through POST/GET/PUT methods.
memory_limit In order to avoid running scripts from using a large amount of system memory, PHP allows you to define memory usage limits. Set this parameter to specify the maximum memory capacity that a single script can use, which should be appropriately larger than the post_max_size value.
max_execution_time is used to set the time, in seconds, that PHP waits for the script to be executed before forcibly terminating the script. This second option can limit infinite loop scripts, but this feature can also cause the operation to fail when there is a long period of legitimate activity (such as uploading a large file). In this case, you must consider increasing this variable.
4. Consider multi-file upload
You can easily upload multiple files by using the $_FILES array. The $_FILES array can obtain all the file field contents in the client form, thereby obtaining all files uploaded in the same form.
5. Break through the upload memory limit
Method 1:
Modify the memory_limit value in php.ini to a larger value, such as 64M
Method 2:
Use the Apache Rewrite method to dynamically modify the value of memory_limit. First create a .htaccess file and save it in the current directory of the file upload program.
The code is as follows:
php_value memory_limit 100M php_value post_max_size 30M php_value upload_max_filesize 30M php_value max_execution_time 300 php_value max_input_tim 300 php_value display_errors On
The above is the detailed content of How to implement upload file function in php. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
