使用 Blob 将图像上传到 MySQL 数据库

2023-03-06php开发问题
1

本文介绍了使用 Blob 将图像上传到 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

首先澄清一下,我知道有更好的方法可以做到这一点,但我决心进行实验.

Just to first clarify, I know there are better ways to do this, but I'm determined to experiment.

基本上,我试图从表单帖子中获取图像,然后将其发送到数据库 MEDIUMBLOB 字段中.

Basically, I'm trying to grab an image from a form post, then send that into a database MEDIUMBLOB field.

不幸的是,使用我当前的方法,数据库列中的最终结果始终为零字节.

Unfortunately, using my current method, the end result in the database column is always zero bytes. phpMyAdmin Screenshot

这里是表单上传图片的代码:

Here is the code for the image upload on the form:

input type="file" name="_imagePost">

这是页面上的 PHP 代码,我使用的是 MySQLi :

Here is the PHP code on the page, I'm using MySQLi :

    if(isset($_POST['_imagePost']))
    {
        $_useImagePost = 1;
        $_imagePost = file_get_contents($_FILES['_imagePost']);

        // Open DB Connection
        $_conn = databaseConnect();

        $_stmt = $_conn->prepare("INSERT INTO Question (Question_Type, Question_Text, Question_Answer, Question_UseImage, Question_Image) VALUES (?, ?, ?, ?, ?)");
        $_stmt->bind_param("sssib", $_typePost, $_textPost, $_answerPost, $_useImagePost, $_null);
        $_stmt->send_long_data(4,$_imagePost);
        $_stmt->execute();

        // Close DB Connection
        $_conn->close();
    }

我不确定的是,当输入类型为文件时,isset($_POST['_imagePost'])"是否有效.

What I am unsure of is if "isset($_POST['_imagePost'])" works when the input type is file.

然而,我可以肯定的是,当前的设置根本不起作用.

What I am sure of, however, is that the current setup doesn't work at all.

推荐答案

你写的:

$_imagePost = file_get_contents($_FILES['_imagePost']);

正确的语法是:

$_imagePost = file_get_contents($_FILES['_imagePost']['tmp_name']);

$_FILES 是一个带有以下键的关联数组:

$_FILES is an associative array whit following keys:

  • [name] => 原始文件名;
  • [type] => 上传文件的 mimetype;
  • [tmp_name] => 临时文件路径(上传文件的存储位置);
  • [错误] => 错误;
  • [size] => 上传文件的大小.
  • 查看更多关于$_FILES

这篇关于使用 Blob 将图像上传到 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17