<!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