PHP multi-file upload and upload file principle analysis

Source: Internet
Author: User
Tags explode file upload http post php tutorial port number

First Use PHP tutorial socket function library to create a temporary HTTP server, listening at one end of the port, and then the IP address and port number to notify the client, the client Upload form submission (temporary server), the temporary server to accept client requests, and read post data, Analyzes and obtains the client uploads the file information, saves the file on the server, then closes the temporary server, releases the resources, uploads completes. A bit around, but the idea is simple.

<?php

Class Upload {
Public $up _ext=array (), $up _max=5210, $up _dir;
Private $up _name, $up _rename=true, $up _num=0, $up _files=array (), $up _ret=array ();

function __construct ($name, $ext =array (), $rename =true) {
if (!empty ($name)) {
$this->up_name = $name;
!empty ($ext) && $this->up_ext = $ext;
$this->up_rename = $rename;
$this->up_dir = Website_dirroot.
$globals [' Cfg_upload_path '];
$this->initupload ();
} else {
Exit (' Upload file domain name is empty, initialization failed! ');
}
}

Private Function Initupload () {
if (Is_array ($_files[$this->up_name])) {
$up _arr = count ($_files[$this->up_name]);
$up _all = count ($_files[$this->up_name], 1);
$up _cnt = ($up _all-$up _arr)/$up _arr;
for ($i = 0; $i < $up _cnt; $i + +) {
if ($_files[$this->up_name][' error '] [$i]!= 4) {
$this->up_files[] = Array (
' Tmp_name ' => $_files[$this->up_name][' tmp_name ' [$i],
' Name ' => $_files[$this->up_name][' name ' [$i],
' Type ' => $_files[$this->up_name][' type ' [$i],
' Size ' => $_files[$this->up_name][' size ' [$i],
' Error ' => $_files[$this->up_name][' ERROR ' [$i]
);
}
}
$this->up_num = count ($this->up_files);
} else {
if (Isset ($_files[$this->up_name])) {
$this->up_files = Array (
' Tmp_name ' => $_files[$this->up_name][' tmp_name '],
' Name ' => $_files[$this->up_name][' name '],
' Type ' => $_files[$this->up_name][' type '],
' Size ' => $_files[$this->up_name][' size '],
' Error ' => $_files[$this->up_name][' ERROR ']
);
$this->up_num = 1;
} else {
Exit (' not find the file that needs upload! ');
}
}

$this->chkupload ();
}

Private Function Chkupload () {
if (Empty ($this->up_ext)) {
$up _mime = Array (' image/wbmp ', ' image/bmp ', ' image/gif ', ' image/pjpeg ', ' image/x-png ');
foreach ($this->up_files as $up _file) {
$up _ALLW = false;
foreach ($up _mime as $mime) {
if ($up _file[' type '] = = $mime) {
$up _ALLW = true; Break
}
}
! $up _allw && exit (' not allowed to upload '. $up _file[' type ']. ' File in a format! ');

if ($up _file[' size ']/1024 > $this->up_max) {
Exit (' Do not allow uploading of files larger than '. $this->up_max. ' K! ');
}
}
} else {
foreach ($this->up_files as $up _file) {
$up _ext = end (Explode ('. ', $up _file[' name '));

$up _ALLW = false;
foreach ($this->up_ext as $ext) {
if ($up _ext = = $ext) {
$up _ALLW = true; Break
}
}
$up _allw && exit (' not allowed to upload. ') $up _ext. ' Format Files! ');

if ($up _file[' size ']/1024 > $this->up_max) {
Exit (' Do not allow uploading of files larger than '. $this->up_max. ' K! ');
}
}
}

$this->uploading ();
}

Private function uploading () {
if (io::d ircreate ($this->up_dir)) {
if (chmod ($this->up_dir, 0777)) {
if (!empty ($this->up_files)) {
foreach ($this->up_files as $up _file) {
if (Is_uploaded_file ($up _file[' Tmp_name ')) {
$file _name = $up _file[' name '];
if ($this->up_rename) {
$file _ext = end (Explode ('. ', $file _name));
$file _rnd = substr (MD5 (UNIQID ()), Mt_rand (0, 26), 6);
$file _name = Date (' Ymdhis '). ' _ '. $file _rnd. '. $file _ext;
}
$file _name = $this->up_dir. '/'. $file _name;

if (Move_uploaded_file ($up _file[' tmp_name '], $file _name)) {
$this->up_ret[] = str_replace (Website_dirroot, ', $file _name);
} else {
Exit (' File upload failed! ');
}
}
}
}
} else {
Exit (' Write permission not turned on! ');
}
} else {
Exit (' Upload directory creation failed! ');
}
}

Public Function Getupload () {
return empty ($this->up_ret)? False: $this->up_ret;
}

function __destruct () {}
}
?>


So the upload process is as follows:

1. Assign a value to the Enctype property of the form label multipart/form-date, which is required and must also be specified, otherwise the file cannot be uploaded.

2. Judge

This judgment generally has three judgments, one is the type judgment, one is the size judgment, one is the document existence judgment. Because these three judgments are guaranteed security and the premise of successful upload.

3. Save uploaded files

Since file uploads are stored in a temporary location, it is also convenient to transfer them to a specified location by means of a method.

said the principle and the process, is the actual operation as well as some important functions.

The first is to specify the file type, but not directly, you must specify a MIME type, or PHP will complain, if you want to know each file suffix corresponding MIME type, you can see here.

The information of uploaded files is generally included in an associative array called $_files, which contains five aspects of information.

$_files[' file ' [' name '] => the original of the file to be uploaded

$_files[' file ' [' type '] => the type of file to upload

Size of $_files[' file ' [' Size '] => file

$_files[' file ' [' tmp_name '] => stored temporary file names, typically the system default

$_files[' file ' [' Error '] => upload related error code

The error code can be a string of numbers, or it can be some constant. such as 0->upload_err_ok-> upload success. You can see the detailed correspondence here.

Before transferring a file, it is best to use the Is_uploaded_file () function to test whether the temporary file was uploaded via an HTTP post, or whether the file exists, typically using the temporary name of the file as an argument. Let's use the Move_uploaded_file () function to transfer uploaded files, this function has two parameters, the first is to specify the file to be transferred, the second is to specify the file to be transferred. The file to be transferred is typically a temporary file, so it is specified with a temporary file name (tmp_name). The second parameter is generally also used as a new file name, stored in the database tutorial, can be called later.

Here to say that a large file upload, without the help of Third-party tools, you can set the php.ini parameters to complete, one is upload_max_filesize, this is the designated upload maximum limit, the default is 2m, there is another one is Max_input_ Time, it specifies the maximum duration of the form submission, because the general large file upload is better, so this is also necessary.

Forms can also have other controls, such as Post_max_size, which controls the maximum acceptable value for a form, and memory_list can specify the maximum memory that each PHP page can hold, by default 8m,max_input_ Time can specify the maximum number of times each PHP page will accept data, and the default is 60 seconds.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.