php files 保存,PHP利用file_put_contents()来保存base64图片上传到服务器

最近又开始干老本行了,长时间没碰都快忘光了,根据需求找到一个很不错的头像选择,然后发现图像保存的是Base64信息,于是乎就来分享个保存上传Base64图片的简单方法。

说白了无论变成什么样的信息,它都是图片,只要把编码逆向还原,就OK了。

$imgBase64 = "图片Base64编码信息";

$img = base64_decode($imgBase64);

file_put_contents('./test.jpg',$img);

?>

file_put_contents()函数是PHP自带的一个字符串写入文件功能的函数,和依次调用 fopen(),fwrite() 以及 fclose() 功能一样。

正式格式为:file_put_contents(file,data,mode,context)

file             必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。

data          可选。规定要写入文件的数据。可以是字符串、数组或数据流。

mode        可选。规定如何打开/写入文件。

context    可选。规定文件句柄的环境,若为空,则可以忽略。

注:此函数mode变量里如果使用 FILE_APPEND可避免删除文件中已有的内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用Base64编码将本地图片保存到数据库。以下是一个示例代码: ```java import java.io.File; import java.nio.file.Files; import java.util.Base64; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Lob; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class ImageToDatabaseApplication { public static void main(String[] args) { SpringApplication.run(ImageToDatabaseApplication.class, args); } } @RestController class ImageController { private final ImageRepository imageRepository; public ImageController(ImageRepository imageRepository) { this.imageRepository = imageRepository; } @PostMapping("/upload") public String uploadImage(@RequestParam("file") File file) { try { // 读取图片文件并进行Base64编码 byte[] fileContent = Files.readAllBytes(file.toPath()); String base64Image = Base64.getEncoder().encodeToString(fileContent); // 创建实体对象并保存到数据库 Image image = new Image(); image.setData(base64Image); imageRepository.save(image); return "图片保存成功!"; } catch (Exception e) { e.printStackTrace(); } return "图片保存失败!"; } } @Entity class Image { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Lob private String data; // getters and setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getData() { return data; } public void setData(String data) { this.data = data; } } interface ImageRepository extends JpaRepository<Image, Long> { } ``` 在这个示例中,我们创建了一个Spring Boot应用程序,使用`ImageController`来处理上传图片的请求。在`uploadImage`方法中,我们首先将文件内容读取为字节数组,然后使用Base64编码转换为字符串。接下来,我们创建一个`Image`实体对象,将Base64编码后的图片数据设置到`data`属性中,并通过`ImageRepository`将实体保存到数据库中。 请确保在`application.properties`文件中配置正确的数据库连接信息,并在pom.xml文件中添加适当的依赖项(例如Spring Data JPA和MySQL驱动程序),以便应用程序能够正常工作。 这是一个简单的示例,你可以根据实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值