PHP Core Programming-File upload (including multi-file upload)

Source: Internet
Author: User
Tags http post

I. Single File Upload

Image upload Interface:

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Document</title></Head><Body>     <formname= ' frm ' action= "./uploadsave.php"Method= "POST"enctype= "Multipart/form-data">         <inputtype= "File"name= "Upfile" ></input>         <inputtype= "Submit"name= "Submit"value= "Upload picture"></input>     </form></Body></HTML>

Upload Processing file:

<?PHPHeader(' Content-type:text/html;charset=utf-8 ');/** * Upload file: You can set the uploaded file type by the input parameters * upload file size limit, and upload the path * @param [type] $myupfile [form file field name] * @param [type] $maxsize [allowed Maximum allowable uploads] * @param [type] $arr _mime [allowed MIME type] * @param [type] $path [uploaded path] * @author Qinxiaoshou * September 6, 2017*/functionUploadFile ($myupfile,$maxsize,$arr _mine,$path) {        //determine if the file was uploaded via HTTP POST//if (Isset ($_post[' submit '))) {}        if(!Is_uploaded_file($myupfile[' Tmp_name '])) {            //Stop the script directly if there is an error            Echo"Upload error, please re-upload"; }        //determine if there is an error        if($myupfile[' Error ']) {            Echo"Upload error, please re-upload"; }        //determine file Size--do not allow uploading of files larger than 2M        if($myupfile[' Size '] >$maxsize) {            Echo"Upload file too large, please re-upload"; }        //get file name extension        $filename=$myupfile[' Name ']; ////Method One//strrpos calculates the last occurrence of the specified string in the target string//strrpos (haystack, needle);        Strrpos (sea, needles);        $index = Strrpos ($filename, '. ');        $ext = substr ($filename, $index);        echo "$ext"; Method Two//STRRCHR-finds the last occurrence of the specified character in the string        $ext=STRRCHR($filename,‘.‘);//. jpg//Echo $ext; Determine file type//process-oriented style        $fs=Finfo_open (Fileinfo_mime_type); $mine= Finfo_file ($fs,$myupfile[' Tmp_name ']); //file types allowed to upload//$arr _mine = [' Image/jpeg ', ' image/png ']; in_array-checks if a value exists in the array//in_array (needle, haystack)        if(!In_array($mine,$arr _mine)) {             die(' The file type is not correct, please re-upload '); }        //Building Temporary Files        $filename=$myupfile[' Tmp_name ']; //build target file//$path = './upload ';        $destination=$path.‘ /‘.Date(' Ymdhis ').$ext; //move the uploaded file to a new location return Boolean//move_uploaded_file (filename,destination)//move_uploaded_file (temporary file, custom destination file)        if(!Move_uploaded_file($filename,$destination)) {             die(' Upload failed! ')); }    }

Two, multi-file upload

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Document</title></Head><Body>    <formname= ' frm 'Method= ' Post 'Action= ' upload.php 'enctype= "Multipart/form-data">        <Tablewidth= "50%"Border= "2"Align= "Center"rules= "All"cellpadding= "Ten">            <TR>                <thcolspan= "2">Upload file:<BR>                <inputtype= "File"name= "upload[]"></input><BR><BR>                <inputtype= "File"name= "upload[]"></input><BR><BR>                <inputtype= "File"name= "upload[]"></input><BR><BR>                <inputtype= "Submit"name= "Submit"value= "Upload"></input>                </th>            </TR>        </Table>    </form></Body></HTML>

Multi-File Upload processing:

<?PHPHeader(' Content-type:text/html;charset=utf-8 ');if(isset($_post[' Submit '])) {    /*echo "<pre>";    Print_r ($_files);    echo "</pre>"; echo "*/    $arr=$_files[' Upload ']; Echo"<pre>"; Print_r($arr); Echo"</pre>"; Echo"; foreach($arr[' Tmp_name '] as $key=$value) {        /*Echo $key, $value, "<br>"; Die;*/        //build temporary file//$filename = $_files[' upload ' [' tmp_name ']; Build the target file            if($arr[' Size '] [$key] >2*1024*1024){                 die(' No '); }            $path= './upload '; //gets a unique ID with a prefix, based on the current time microseconds.             $destination=$path.‘ /‘.uniqid(' Itcast '). jpg; //move uploaded file to new location, return boolean//move_uploaded_file (filename, destination)//move_uploaded_file (temporary file, custom destination file )            if(!Move_uploaded_file($value,$destination)){                 die(' Upload failed! ')); }    }    } Else {    //If you visit this page directly    Echo"<script>alert (' illegal uploading '); location.href= ' index.php ' </script>";}?>

PHP Core Programming-File upload (including multi-file upload)

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.