【问题标题】:Retrieving image from MySQL database从 MySQL 数据库中检索图像
【发布时间】:2018-02-12 05:20:33
【问题描述】:

我正在使用下面的代码从 mysql 数据库上传和检索图像。图像已成功上传并移动到单独的文件夹,但图像路径未存储在数据库中,因此我无法从数据库中检索图像。请检查问题出在哪里

if(isset($_POST["submit"])){
              $check = getimagesize($_FILES["image"]["tmp_name"]);
            if($check !== false){
              $target="images/".basename($_FILES["image"]["tmp_name"]);
              $image = $_FILES['image']['tmp_name'];
              $imgContent = addslashes(file_get_contents($image));
              $uploadfilename=$_FILES['image']['name'];
        /*
         * Insert image data into database
         */

       $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
        $dataTime = date("Y-m-d H:i:s");

        //Insert image content into database
          $insert = $db->query("INSERT into images (image,created)   VALUES('$imgContent', '$dataTime')");
      //move uploaded file to the folder images//


      if($uploadfilename!=''){
        move_uploaded_file($_FILES["image"]["tmp_name"],$target)
          $query="INSERT INTO images SET imgepath='$uploadfilename' ";
        mysqli_query($query);
        if($query){

           { 
            echo "File uploaded  successfully.";
           }
        }else{
            echo "File upload failed, please try again.";
        } 
    }else{
        echo "Please select an image file to upload.";
    }
    }
    }
   }
    ?>

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    将图像名称保存在数据库中并在服务器中上传图像,同时检索您可以使用文件夹名称后跟图像名称。比如上传图片的文件夹名称是upload

    <img src="uploads/<?= $v1['image']?>" alt="product">

    【讨论】:

    • 仅供参考 <?= 仅在为 php 启用短标签时才有效,最好使用 <?php echo $v1['image'] ;?>
    【解决方案2】:

    你应该把图片的文件夹路径或者你的$target变量而不是$imgContent

    "INSERT into images (image,created) VALUES('$target', '$dataTime')"
    

    这也容易发生 SQL 注入,请使用准备好的语句:

    // connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // prepare and bind
    $stmt = $db->prepare("INSERT into images (image,created) VALUES(?, ?)");
    
    $stmt->bind_param("ss", $target, $dataTime);
    
    //execute
    $stmt->execute();
    

    【讨论】:

      【解决方案3】:

      感谢您的帮助。下面的代码正在运行。我创建了一个名为“images”的数据库表,其中包含“id”、“name”(我保存图像名称的位置)和“url”

      <?php
      
      if(isset($_POST['submit']))
      { 
          $host = "...";
          $db_name = "....";
          $username = "....";
          $password = ".....";
          $link=mysqli_connect($host, $username, $password, $db_name);
          $filename=$_FILES['file']['name'];
          $filetmp=$_FILES['file']['tmp_name'];
          $target="uploaded/".$filename;
          $image_url="http://holo-com.stackstaging.com/uploaded".$filename;
          $sql="INSERT INTO images (name,image_url) VALUES  ('$filename','$image_url')";
          mysqli_query($link,$sql);
          if(move_uploaded_file($filetmp,$target))
          {   echo "Image uploaded";  }
      }
      else
      {   echo "Failed to upload";    }
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2021-10-07
        • 2014-02-19
        • 2015-07-06
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多