Detailed explanation of the application and principle of php file upload_PHP tutorial

WBOY
Release: 2016-07-13 10:50:22
Original
833 people have browsed it

You must know several tricks to upload files in php. One must be the data posted by the form, then accepted by php move_uploaded_file, and then saved to the specified path on the server.

1.form tag enctype attribute.

2.$_FILES system function. //Convert the uploaded content into an array.

3.move_uploaded_file function. //Move the uploaded files stored in the cache folder to the specified folder.

4.is_uploaded_file function. //Determine whether it exists.

------------------------------------------

1.form tag

Format:


       

2.$_FILES system function
$_FILES['name'] //The original file name of the file uploaded by the client.
$_FILES['type'] //MIME type of file, such as: "image/gif"
$_FILES['size'] //Upload file size in bytes.
$_FILES['tmp_name'] //Temporary file name, usually the default.
$_FILES['error'] //Upload related situation code (0: Success, 1: Exceeds the size set by php.ini. 2: Exceeds the size specified by the PHP file code. 3: Only part of the file is uploaded. 4: No file is uploaded .5: The uploaded file size is 0)

3.move_uploaded_file function
Function to move files to the target location after uploading
move_uploaded_file (temporary file, target location and file name;)

4.is_uploaded_file function
Function to determine the uploaded MIME type of file
is_uploaded_file(MIME);

------------------------------------------

Example:

The code is as follows Copy code
 代码如下 复制代码


  

 

Attention

1. enctype="multipart/form-data" must be specified in the form to let the server know that the file contains regular form information.
2. There must be a form area where the maximum length of the uploaded file can be set, that is, the maximum value of the uploaded file (calculated in bytes). It is a hidden value field, that is, max_file_size. By setting its Value (value), the uploaded file can be limited. size, to avoid the hassle of users spending time waiting for a large file to be uploaded only to discover that the file is too large. But generally others can bypass this value, so for safety reasons, it is best to configure the upload_max_filesize option in the php.ini file to set the size of the file upload. The default is 2M

The code is as follows Copy code
 代码如下 复制代码

function uploadfile($type,$name,$ext,$size,$error,$tmp_name,$targetname,$upload_dir)
{
    $MAX_SIZE = 2000000;
    $FILE_MIMES = array('image/pjpeg','image/jpeg','image/jpg','image/gif','image/png');
    $FILE_EXTS = array('.jpg','.gif','.png','.JPG','.GIF','.PNG');

    $file_path = $upload_dir.$targetname;
   
    if(!is_dir($upload_dir))
    {
        if(!mkdir($upload_dir))
            die("文件上传目录不存在并且无法创建文件上传目录");
        if(!chmod($upload_dir,0755))
            die("文件上传目录的权限无法设定为可读可写");
    }
   
    if($size>$MAX_SIZE)
        die("上传的文件大小超过了规定大小");

    if($size == 0)
        die("请选择上传的文件");

    if(!in_array($type,$FILE_MIMES) || !in_array($ext,$FILE_EXTS))
        die("请上传符合要求的文件类型");

    if(!move_uploaded_file($tmp_name, $file_path))
        die("复制文件失败,请重新上传");

    switch($error)
    {
        case 0:
            return ;
        case 1:
            die("上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值");
        case 2:
            die("上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值");
        case 3:
            die("文件只有部分被上传");
        case 4:
            die("没有文件被上传");
    }
}

function uploadfile($type,$name,$ext,$size,$error,$tmp_name,$targetname,$upload_dir)

{ $MAX_SIZE = 2000000; $FILE_MIMES = array('image/pjpeg','image/jpeg','image/jpg','image/gif','image/png');

$FILE_EXTS = array('.jpg','.gif','.png','.JPG','.GIF','.PNG');
 代码如下 复制代码

只需将


 

改成



$file_path = $upload_dir.$targetname;   If(!is_dir($upload_dir)) {           if(!mkdir($upload_dir)) die("The file upload directory does not exist and the file upload directory cannot be created"); If(!chmod($upload_dir,0755)) ​​​​​​die("The permissions of the file upload directory cannot be set to read and write"); }   If($size>$MAX_SIZE) ​​​​die("The uploaded file size exceeds the specified size"); if($size == 0)            die("Please select the file to upload"); if(!in_array($type,$FILE_MIMES) || !in_array($ext,$FILE_EXTS))             die("Please upload a file type that meets the requirements"); if(!move_uploaded_file($tmp_name, $file_path))               die("Failed to copy file, please upload again"); switch($error) { case 0: Return ; case 1:                     die("The uploaded file exceeds the value limited by the upload_max_filesize option in php.ini"); case 2: die("The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form"); case 3: die("Only part of the file was uploaded"); case 4:                die("No files were uploaded"); } }
How to upload multiple files? For example, upload 3 files at the same time
The code is as follows Copy code
Just replace changed to

Correspondingly, when calling this function, $_FILES['userfile']['name'][0] represents the relevant file information of the first file, and so on, and the same for others.

Some php performance configurations, we can modify them if necessary

max_execution_time = 30; The maximum time value (seconds) for each PHP page to run, the default is 30 seconds
max_input_time = 60; The maximum time required for each PHP page to receive data, the default is 60 seconds
memory_limit = 128m; The maximum memory consumed by each PHP page, the default is 128M. If it feels too small, you can set it larger. 128 is enough.
max_execution_time = 600
max_input_time = 600
upload_max_filesize = 32m
post_max_size = 32m

If the file size is limited we can solve it as follows

Open php.ini and first find

file_uploads = on ; Switch whether to allow file uploads via HTTP. The default is ON

upload_tmp_dir; Files are uploaded to the server where temporary files are stored. If not specified, the system default temporary folder will be used

upload_max_filesize = 8m; Wangwen business, that is, the maximum value of the file size allowed to be uploaded. Default is 2M

post_max_size = 8m; refers to the maximum value that can be received through POST to PHP through the form, including all values ​​in the form. The default is 8M

Generally, after setting the above four parameters, uploading a file of

But if you want to upload a large file >8M, it will definitely work if you only set the above four items.


Further configure the following parameters

max_execution_time = 600; The maximum time value (seconds) for each PHP page to run, the default is 30 seconds

max_input_time = 600; The maximum time required for each PHP page to receive data, the default is 60 seconds

memory_limit = 8m; The maximum memory consumed by each PHP page, the default is 8M

After modifying the above parameters, you can upload large files under normal circumstances allowed by the network

max_execution_time = 600
max_input_time = 600
memory_limit = 32m
file_uploads = on
upload_tmp_dir = /tmp
upload_max_filesize = 32m
post_max_size = 32m

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632639.htmlTechArticleYou must know several tricks to upload files in php. One must be to post the data in the form, and then move_uploaded_file by php After accepting it, save it to the specified path on the server. ...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template