【问题标题】:How to upload ANY file type BLOB using PHP如何使用 PHP 上传任何文件类型的 BLOB
【发布时间】:2022-03-31 21:03:40
【问题描述】:

美好的一天。 因此,我正在努力将 blob 插入从表单发送的 mySQL 数据库中。无论如何,它在插入时失败,当我使用 var_dump 查看 $sql 变量时,我会收到此屏幕。 我的最后一点是将任何类型的文件插入数据库,无论大小。请注意,我正在尝试使用任何文件类型(zip、docx 或其他)来执行此操作。

这是我的表格

<form name="frmImage" enctype="multipart/form-data" action="upload.php"method="post" >
    <label>Upload File:</label><br /> 
    <input autocomplete="off" type="text" id="notes" style="width:50%" name="notes" placeholder="Write Something about this upload">
    <input id="myfile" name="myfile" type="file"/> 
    <button style="width:20%" id="sendFile" name="sendFile" type="sendFile" value="sendFile">Submit</button>
</form>

这里是处理表单输入的 PHP 脚本。

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');  //On or Off

 session_start();
 require_once('connect.php');

    if (isset($_POST['sendFile'])) {

            $imgData = ($_FILES['myfile']['tmp_name']);
            $imageProperties = getimageSize($_FILES['myfile']['tmp_name']);
            $notes = mysqli_real_escape_string($connection,$_POST['notes']);
            $email = mysqli_real_escape_string($connection, $_SESSION['email']);
            $comentity = mysqli_real_escape_string($connection,$_SESSION['entityname']);
            $file_size = $_FILES['myfile']['size'];

            $sql = "INSERT INTO `supportcontent` (`sentity`,`scontentdata`, `scontentnotes`, `suseremail`, `stype`,`ssize`)
            VALUES('$comentity', '".file_get_contents($imgData)."','$notes','$email','{$imageProperties['mime']}','$file_size')";

            var_dump($sql);
            $current_id = mysqli_query($connection, $sql) 
            or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error($connection));
            if (isset($current_id)) {
                header("Location: landingpage.php?upload=success");
            }               
        exit();
    }
    else{
        echo 'pedestrian';
    }

?>

所以我不确定出了什么问题,我知道问题正在我的插入中发生,但我不确定是什么原因造成的。

【问题讨论】:

标签: php mysql


【解决方案1】:

您可能想根据这个答案尝试 base64_encode 函数:Error in SQL syntax when trying to upload image to BLOB field

【讨论】:

    【解决方案2】:

    我曾经遇到过同样的问题,但这段代码在我身上运行良好: 请记住将存储文件的列更改为 longblob

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-11
      • 2016-03-23
      • 2014-03-29
      • 2019-12-30
      • 2013-12-16
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多