Php implements the Avatar preview upload function, and php Avatar preview upload
The portrait upload function is required for the php Phase 2 project recently.
We need to complete the Avatar upload function. We need to write two php pages in total. The first page is called touxiang. php, and the second page is called upload. php.
1. touxiang. php
1 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2
On this page, we need to introduce three files: A schema and bootstrap.min.css, jquery-1.11.2.min.js, bootstrap. min. js.
2. upload. php
1 <? Php 2 3 if ($ _ FILES ["file"] ["error"]) 4 {5 echo $ _ FILES ["file"] ["error"]; 6} 7 else 8 {9 if ($ _ FILES ["file"] ["type"] = "image/jpeg" | $ _ FILES ["file"] ["type"] = "image/png ") & $ _ FILES ["file"] ["size"] <1024000000) 10 {11 $ fname = ". /img /". date ("YmdHis "). $ _ FILES ["file"] ["name"]; 12 13 $ filename = iconv ("UTF-8", "gb2312", $ fname ); 14 15 if (file_exists ($ filename) 16 {17 echo "<script> alert ('this file already exists! '); </Script> "; 18} 19 else20 {21 move_uploaded_file ($ _ FILES [" file "] [" tmp_name "], $ filename ); 22 23 unlink ($ _ POST ["tp"]); 24 25 echo "<script> parent. showimg ('{$ fname}'); </script> "; 26} 27 28} 29}
The webpage displays the following results:
Okay, so you can upload a simple avatar. Please try it!