SoFunction
Updated on 2025-03-09

PHP implements the method of saving base64 format images in a specified directory

This article describes the method of php to save base64 format images in a specified directory. Share it for your reference, as follows:

<?php
header('Content-type:text/html;charset=utf-8');
$base64_image_content = $_POST['imgBase64'];
//Match the format of the pictureif (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$new_file = "upload/active/img/".date('Ymd',time())."/";
if(!file_exists($new_file))
{
//Check whether the folder is present, create it if it is not, and give the highest permissionsmkdir($new_file, 0700);
}
$new_file = $new_file.time().".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
echo 'New file saved successfully:', $new_file;
}else{
echo 'New file failed to save';
}
}
?>

PS: Here are a few online picture tools for your reference

Image conversion to Base64 encoding online tool:
http://tools./transcoding/img2base64

Online Email Email Icon Creation Tool:
http://tools./email/emaillogo

Online PS image processing tool:
http://tools./aideddesign/webps

Online picture format conversion (jpg/bmp/gif/png) tool:
http://tools./aideddesign/picext

ICO icon online generation tool:
http://tools./aideddesign/ico_img

For more information about PHP related content, please check out the topic of this site:Summary of PHP graphics and picture operation skills》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《PHP object-oriented programming tutorial》、《Summary of PHP network programming skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.