【问题标题】:Storing image file in postgresql table failed在 postgresql 表中存储图像文件失败
【发布时间】:2019-04-09 16:01:05
【问题描述】:

我正在尝试将图像文件存储在 psql 数据库表中。表 'images' 有两个字段,'id' 类型为 integer 和 'image' 类型为 bytea。

我尝试通过网络教程修改图像保存代码。但我的代码正在引发错误和警告。

这是错误信息:

Notice: Undefined index: image in C:\xampp\htdocs\maps\un.php on line 48

Warning: fopen(): Filename cannot be empty in C:\xampp\htdocs\maps\un.php on line 50
cannot read image 

这些错误出现在 if(isset($_POST["submit1"])){ 块内的行上

如何纠正这些错误?

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Image Upload</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

</head>
<body>
<?php
require("connection.php");
?>

<div class="container-fluid">
          <h1 align="center">Test Form </h1>

   <form action="" method="post" class="needs-validation" novalidate enctype="multipart/form-data"> 
    <h2> Field </h2>
  <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom01">Field ID</label>
      <input type="number" class="form-control" name="field_id" id="field_id" placeholder="insert field id" value="" required>
      <div class="valid-feedback">
        0
      </div>
    </div>
      <div class="col-md-4 mb-3">
      <label for="validationCustom02">IMG</label>
      <input type="file" class="form-control" name="image" id="image" placeholder="insert image" value="" required>
      <div class="valid-feedback">

      </div>
    </div>      
   </div>

  <button class="btn btn-primary" name="submit1" type="submit">Insert Image</button>
</form>


    <?php

          if(isset($_POST["submit1"])){

              $file_name = $_POST["image"];

              $img = fopen($file_name, 'r') or die("cannot read image\n");
              $data = fread($img, filesize($file_name));

              $es_data = pg_escape_bytea($data);
              fclose($img);   


            try {

                $sql = "INSERT INTO images (field_id, image)
                VALUES ('".$_POST["field_id"]."','.$es_data.')";
                 // echo "<meta http-equiv='refresh' content='0'>";
                if ($conn->query($sql)) {
                     echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
                          }
                          else{
                            echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
                      }
                            $dbh = null;
                    }
                        catch(PDOException $e)
                    {
                        echo $e->getMessage();
                        }
                    }   

                    //$conn = null;

                    ?>

    </div>    
</body>
</html> 

【问题讨论】:

  • 您是否有理由将整个图像存储在数据库中?为什么不只存储路径?
  • @Lithilion:因为图片分辨率不高,数量也不多。
  • 但是将图像存储在数据库中确实会减慢速度...

标签: php html postgresql image upload


【解决方案1】:

文件存储在$_FILES 超全局中。 $_POST中没有图片。
文件名将是$_FILES["image"]["tmp_name"]。这是php临时上传的路径,你可以自己处理。

参考:https://secure.php.net/manual/en/reserved.variables.files.php
https://secure.php.net/manual/en/features.file-upload.php

PS:您的代码容易受到 SQL 注入的攻击。请使用Prepared Statements或PDO。

【讨论】:

  • 谢谢:未定义索引:图像错误消失但存在此警告:警告:fopen() 期望参数 1 是有效路径,数组在 C:\xampp\htdocs\maps\un 中给出。第 50 行的 php 无法读取图像。第 50 行是:$data = fread($img, filesize($file_name));
  • 你确定它是正确的吗?它说了一些关于 fopen 的内容,但它不在您显示的行中
  • 对不起; $img = fopen($file_name, 'r') 或 die("无法读取图像\n");
  • 你能var_dump($file_name)吗?
  • 在第 50 行之前执行并注释下面的行。
猜你喜欢
  • 2015-07-01
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 2022-01-27
  • 2017-06-02
  • 2019-05-07
相关资源
最近更新 更多