PHP Upload file Code exercise

Source: Internet
Author: User

Front:

<HTML>    <Head><title>Upload file</title>    <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8"/>    </Head>    <Body>        <formAction= "uploadtest.php"Method= "POST"enctype= "Multipart/form-data">            <!--name must be Max_file_size,value is the number of bytes -            <inputtype= "hidden"name= "Max_file_size"value= "2097152"/>            <!--Accept is the MIME type of the file -            <inputtype= "File"name= "FileName"Accept= "image/jpeg,image/gif,image/png,application/x-ms-bmp,text/plain,text/html" />            <inputtype= "Submit"value= "Upload file"/>        </form>    </Body></HTML>

Functions for uploading files:

<?PHPHeader(' Content-type:text/html;charset=utf-8 ');functionUploadFile ($file,$uploadpath= ' upload ',$extarray=[' jpg ', ' jpeg ', ' gif ', ' PNG ', ' BMP ',$max=2097152,$flag=false){    //[email protected]$_files[' fileName '];    Var_dump ($file); $max = 2097152;    Added to the parameter, the default 2M//due to the client limit is unreliable, so also on the server to restrict. Limit the size of the file;    if($file[' Size ']>$max) {        Exit(' The file is too large to be uploaded. ‘); #code ...    }    if($file[' Error ']!==0) {        Switch($file[' Error ']){             Case1:$errmsg= "The upload file exceeds the value of the Upload_max_filesize option limit in php.ini. ";  Break;  Case2:$errmsg= "The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form." "; #code ...                    Break;  Case3:$errmsg= "file is only partially uploaded"; #code ...                    Break;  Case4:$errmsg= "No files are uploaded"; #code ...                    Break;  Case6:$errmsg= "The Temp folder could not be found." "; #code ...                    Break;  Case7: Case8:$errmsg= "System Error"; #code ...                    Break; }        Exit($errmsg); #code ...}Else{        //determine if it was uploaded via HttpPost        if(!Is_uploaded_file($file[' Tmp_name '])) {            Exit(' not upload via HTTP POST '); #code ...        }        //determine file format//can also be used PathInfo ($file [' name '],pathinfo_extension);        $ext[Email protected]Strtolower(End(Explode(‘.‘,$file[' Name ']))); //$extarray =[' jpg ', ' jpeg ', ' gif ', ' PNG ', ' BMP '];//added to the array, custom file type        if(!In_array($ext,$extarray)) {            Exit(' File format error '); #code ...        }        //Judging whether it's a real picture        if($flag===true){            if(@!getimagesize($file[' Tmp_name '])) {                Exit(' Not a real picture '); #code ...            }        }        //Specify the upload directory//$uploadpath = ' upload ';//Add the default value to the parameter ' upload '//Determine if the file exists        if(!file_exists($uploadpath)) {            mkdir($uploadpath, 0777,true); chmod($uploadpath, 0777); #code ...        }                //ensure that each uploaded file has a unique name        $newname=MD5(uniqid(Microtime(true),true)).‘.‘.$ext; $pathname=$uploadpath.‘ /‘.$newname; if(Move_uploaded_file($file[' Tmp_name '],$pathname)) {            //exit (' upload successful '); # code ...            return $pathname; }    }}?>

Using functions:

<? PHP include (' upload.func.php '); $file=$_files[' FileName ']; $new=uploadfile ($file, ' Lijian ',$extarray=[' txt ', ' html ']); Echo $new;

PHP Upload file Code exercise

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.