How to use PHP to upload files, upload images, php upload tutorials, php form file upload Tutorial

Source: Internet
Author: User
Tags php form

Use PHP for file upload, mainly use the form function and PHP built-in $_files function function. Next we see how to implement PHP upload function. Example, this example is successful in debugging under a Mac.

PHP Upload image File function code is as follows:

FileExample</title> <style>body{Width:500px; Margin:20px Auto; Line-height:25px; } img{Display:Block; Max-width:500px; } h1{Text-align:Center; }        #uploadfile{Background-color:#eee;Text-align:Center; Width:500px; Margin: 0Auto; } form{margin:0px; Padding:0px; }        #uploadfile. input1{DisplayBlock; Width:500px; Text-align:Center; Margin: 0Auto; }        #uploadfile. input2,.input3{Width100px; Margin:5px 20px; }    </style>File$_server[' php_self '];? > "method=" POST "> <inputclass= "INPUT1" name= "upfile" type= "file" > <inputclass= "Input2" type= "Submit" value= "confirm Submission" > <inputclass= "INPUT3" type= "reset" value= "reset" > </form> </div> <?PHPif($_files["Upfile"] ["Name"]==NULL){            Echo"No file choice!"; }Else{            $filepath= "/library/webserver/documents/"; $tmp _name=$_files["Upfile"] ["Tmp_name"]; $filename=$filepath.$_files["Upfile"] ["Name"]; Echo"<br>". \$_files[' upfile ' [' Name ']: ".$_files["Upfile"] ["Name"]; Echo"<br>". \$_files[' upfile ' [' Size ']: ". GetSize ($_files["Upfile"] ["Size"]); Echo"<br>". \$_files[' upfile ' [' type ']: ".$_files["Upfile"] ["Type"]; Echo"<br>". \$_files[' upfile ' [' Tmp_name ']: ".$_files["Upfile"] ["Tmp_name"]; if(Move_uploaded_file($tmp _name,$filename)){                Echo"<br>". Uploaded to Directory: ".$filename; if(In_array($_files["Upfile"] ["Type"],Array("Image/png", "image/jpg", "Image/bmp"))){                    Echo"<br>". $_files["Upfile"] ["Name"]. "' /> "; }Else{                }            }Else{                Echo"<br>". Upload faild! "; }        }//convert file size to corresponding unit of measure        functionGetSize ($tmp){            $arr=Array("B", "KB", "MB", "GB", "TB", "PB"); $pos= 0; if(!Is_integer($tmp))                return-1;  while($tmp>1024 andCount($arr) >$pos){                $tmp=$tmp/1024.0; $pos++; }            $tmp=sprintf("%.2f",$tmp); return $tmp.$arr[$pos]; }    ?></body>

Use the form's action to submit an object to a form, like the example in this article, which submits the form to itself for processing.

Method can select a post or get file for submission.

How to use PHP to upload files, upload images, php upload tutorials, php form file upload Tutorial

Related Article

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.