Php implements base64 Image Upload instance code,
The File Post upload method is not used in this example! The principle is the same. For a better understanding of base64, select to output it to the text domain and submit it to the server! We recommend that you submit a File for the project.
Html code
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
PHP Functional Block Code
<? Php/*** base64 Image Upload * @ param $ base64_img * @ return array */$ base64_img = trim ($ _ POST ['img ']); $ up_dir = '. /upload/'; // store it in the upload folder of the current directory if (! File_exists ($ up_dir) {mkdir ($ up_dir, 0777);} if (preg_match ('/^ (data: \ s * image \/(\ w + ); base64,)/', $ base64_img, $ result) {$ type = $ result [2]; if (in_array ($ type, array ('pjpeg', 'jpeg ', 'jpg ', 'gif', 'bmp', 'png ') {$ new_file = $ up_dir.date ('ymdhis _'). '. '. $ type; if (file_put_contents ($ new_file, base64_decode (str_replace ($ result [1], '', $ base64_img) {$ img_path = str_replace ('.. /.. /.. ', '', $ new_file); echo 'Image uploaded successfully </br>! [] ('. $ Img_path. ')';} else {echo 'image Upload Failed </br> ';} else {// file type error echo 'image upload Type Error ';}} else {// file error echo 'file error ';}
The instance effect is as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.