Php implements base64 Image Upload instance code,

Source: Internet
Author: User

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.

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.