PHP Simple image upload

Source: Internet
Author: User

<!doctype html>class= "Container" >             <form enctype= " Multipart/form-data "action="upload.php?action=img"method=" post "name=" Imge ">                    <input value=" Import Files "type=" file "Name=" img "id=" file "/>                    class=" btn "value=" Upload picture "/>             </form>      </div>    </body>

<?PHPHeader("Content-type:text/html;charset=utf-8");if(isset($_get["Action"]) = = "img") {    //Step 1 Check for errors using $_files[' pic ' ["Error"]    if($_files["img" ["error"] > 0) {        Switch($_files["IMG"] ["Error"]) {             Case1:Echo"<script type= ' text/javascript ' >alert (' uploaded file exceeds php.ini option limit in Upload_max_filesize <br> '); history.back ();</script> ";  Break;  Case2:Echo"<script type= ' text/javascript ' >alert (' the size of the uploaded file exceeds the value specified in the HTML form max_file_size option '); History.back (); </script > ";  Break;  Case5 |Echo"<script type= ' text/javascript ' >alert (' files are only partially uploaded '); History.back ();</script>";  Break;  Case4:Echo"<script type= ' text/javascript ' >alert (' No files were uploaded '); History.back ();</script>";  Break; default:Echo"<script type= ' text/javascript ' >alert (' Last known error '); History.back ();</script>"; }        Exit; }    $maxsize = 1000000000; //Step 2 uses $_files["pic" ["size"] to limit the size unit byte 2m=2000000    if($_files["IMG"] ["Size"] >$maxsize) {        Echo"<script type= ' text/javascript ' >alert (' the uploaded file is too large to exceed {$maxsize} bytes '); History.back ();</script> "; Exit; }    //Step 3 uses $_files["pic" ["type"] or file extension restriction type MIME image/gif image/png gif png jpg    $allowtype=Array("PNG", "gif", "JPG", "JPEG"); $arr=Explode(".",$_files["IMG"] ["Name"]); $hz=$arr[Count($arr)-1]; if(!In_array($hz,$allowtype)) {        Echo"<script type= ' text/javascript ' >alert (' This is an disallowed type '); History.back ();</script>"; Exit; }    //Step 4 will rename the file name after the transfer    $filepath= "uploads/"; //in order to conform to the UBB path    $randname=Date("Y").Date("M").Date("D").Date("H").Date("I").Date("S").Rand(100, 999). "." .$hz; //move files from the temporary location to the specified directory    if(Is_uploaded_file($_files["IMG"] ["Tmp_name"])) {        if(Move_uploaded_file($_files["IMG"] ["Tmp_name"],$filepath.$randname)) {            Echo"<script type= ' text/javascript ' >alert (' upload success '); History.back ();</script>"; Session_Start(); $_session[' images '] =$filepath.$randname; //echo $_session[' images '];}Else {            Echo"<script type= ' text/javascript ' >alert (' upload failed '); History.back ();</script>"; }    } Else {        Echo"<script type= ' text/javascript ' >alert (' not an upload file '); History.back ();</script>"; }}?>

Reference: http://www.sucaihuo.com/php/210.html

PHP Simple image 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.