Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将图像直接插入数据库(php)?_Php_Html_Image - Fatal编程技术网

如何将图像直接插入数据库(php)?

如何将图像直接插入数据库(php)?,php,html,image,Php,Html,Image,我知道不建议这样做,但我需要直接将图像插入msql数据库 这个领域已经像一团了 代码:插入 public function insert($tableName, array $data){ $stmt = $this->pdo->prepare("INSERT INTO $tableName (".implode(',', array_keys($data)).") VALUES (".implode(',', array_fill(0, count($da

我知道不建议这样做,但我需要直接将图像插入msql数据库

这个领域已经像一团了

代码:插入

 public function insert($tableName, array $data){
    $stmt = $this->pdo->prepare("INSERT INTO $tableName (".implode(',', array_keys($data)).")
        VALUES (".implode(',', array_fill(0, count($data), '?')).")"
    );
    try{
        $stmt->execute(array_values($data));
        return $stmt->rowCount();
    } catch (\PDOException $e) {
        throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
    }
}
代码:数据

if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){
extract($_REQUEST);
    $userCount  =   $db->getQueryCount('tb_user','id');
    if($userCount[0]['total']){
           $Photo = $_FILES['in_Photo'];
           $contentFile = file_get_contents($in_Photo['tmp_name']);
        $data = array(
                'C_Imagem'=>$contentFile,
                'C_user'=>$in_Name,
        );
        $insert =   $db->insert('tb_cruds',$data);
        if($insert){
            header('location:index.php?msg=ras');
            exit;
        }else{
            header('location:index.php?msg=rna');
            exit;
        }
    }else{
        header('location:'.$_SERVER['PHP_SELF'].'?msg=dsd');
        exit;
    }
}

代码:Html

<div class="form-group">
    <label>Photo</label>
    <input class="" type="file" name="in_Photo" id="in_Photo"   />
</div>

照片
  • 首先更改数据库的排序规则=>utf8mb4\u常规\u ci
  • 文件大小应相应调整
    文本可存储65535个字符(约64KB) MEDIUMTEXT=16777215个字符(约16 MB) 长文本=4294967295字符(约4GB) VARCHAR最多可以存储255个字符
  • 首先更改数据库的排序规则=>utf8mb4\u常规\u ci
  • 文件大小应相应调整
    文本可存储65535个字符(约64KB) MEDIUMTEXT=16777215个字符(约16 MB) 长文本=4294967295字符(约4GB) VARCHAR最多可以存储255个字符

  • 既然你知道不推荐,为什么不推荐呢?我建议您将图像保存到一个文件夹,并保存它们的名称,如果需要,可能是路径。这样,数据库的负载就更少了。如何在数组中进行存储,在这种情况下,数组是$data?既然知道不是,为什么不建议这样做?我建议您将图像保存到一个文件夹,并保存它们的名称,如果需要,可能是路径。这样,数据库的负载就更少了。如何在数组中存储,在这种情况下,数组是$data?