简体   繁体   English

如何使用 codelgniter php 和我的 sql 从数据库中存储和获取文件

[英]how to store & get file from database using codelgniter php & my sql

i can store the file into database but can't get / display the file stored in database我可以将文件存储到数据库中,但无法获取/显示存储在数据库中的文件

the file is stored in the database but can't display from the database该文件存储在数据库中,但无法从数据库中显示

i am using following code我正在使用以下代码

<?php

class file_upload extends CI_Controller
{

    public function index()
    {
        echo "  <head>
        <title>Upload Image To Database</title>
      </head>
      <body>
        <!-- (A) HTML FILE UPLOAD FORM -->
        <form method='post' enctype='multipart/form-data'>
          <input type='file' name='upload' accept='.png,.gif,.jpg,.webp' required/>
          <input type='submit' name='submit' value='Upload Image'/>
        </form>";

        if (isset($_FILES['upload'])) 
        {
          $imgname=$_FILES["upload"]["name"];
          $data["img_name"]=$_FILES['upload']['name'];
          $data["img_data"]=file_get_contents($_FILES["upload"]["tmp_name"]);
          $this->db->insert('images',$data);
          $data=$this->db->query('select * from images where img_name="$imgname"');
          foreach ($data->result() as $row)
          {
            print_r($row->img_data);
            echo "<img src='data:image/jpg;charset=utf8;base64, ";
            echo "base64_encode($row->img_data)";
            echo "height=100 width=100>";
          }

        }
    }  
}

?>

You were close.你很亲密。 You're base64_encode cannot be executed in a string interpolation like that.你的base64_encode不能在这样的字符串插值中执行。 You can't run functions inside a string interpolation at all.您根本无法在字符串插值中运行函数。 This should work.这应该有效。

<?php

class file_upload extends CI_Controller
{

    public function index()
    {
        echo "  <head>
        <title>Upload Image To Database</title>
      </head>
      <body>
        <!-- (A) HTML FILE UPLOAD FORM -->
        <form method='post' enctype='multipart/form-data'>
          <input type='file' name='upload' accept='.png,.gif,.jpg,.webp' required/>
          <input type='submit' name='submit' value='Upload Image'/>
        </form>";

        if (isset($_FILES['upload'])) 
        {
          $imgname=$_FILES["upload"]["name"];
          $data["img_name"]=$_FILES['upload']['name'];
          $data["img_data"]=file_get_contents($_FILES["upload"]["tmp_name"]);
          $this->db->insert('images',$data);
          $data=$this->db->query('select * from images where img_name="$imgname"');
          foreach ($data->result() as $row)
          {
            print_r($row->img_data);
            echo "<img src='data:image/jpg;charset=utf8;base64, ";
            echo base64_encode($row->img_data);
            echo "'height=100 width=100>";
          }

        }
    }  
}

?>

Also, since you've hard coded jpg , if you've uploaded anything other than that it may not show in the image tag correctly.此外,由于您已经对jpg进行了硬编码,因此如果您上传了除此之外的任何内容,它可能无法正确显示在图像标签中。

Overall it would be better to store the image in a directory somewhere and then store, in your database, information about the image.总的来说,最好将图像存储在某个目录中,然后在数据库中存储有关图像的信息。 type, width, height, location, etc.类型、宽度、高度、位置等

Also, since you're using CI I recommend checkout out their handy functions for handling files.此外,由于您使用的是 CI,因此我建议您查看其处理文件的便捷功能。 Specifically https://www.codeigniter.com/user_guide/incoming/incomingrequest.html?highlight=file%20upload#uploaded-files and https://www.codeigniter.com/user_guide/helpers/filesystem_helper.html Specifically https://www.codeigniter.com/user_guide/incoming/incomingrequest.html?highlight=file%20upload#uploaded-files and https://www.codeigniter.com/user_guide/helpers/filesystem_helper.html

Happy coding!快乐编码!

some quotation (" " / ' ') problem so use following code一些引号(“”/'')问题所以使用下面的代码

<?php

class file_upload extends CI_Controller
{

    public function index()
    {
        echo "  <head>
        <title>Upload Image To Database</title>
      </head>
      <body>
        <!-- (A) HTML FILE UPLOAD FORM -->
        <form method='post' enctype='multipart/form-data'>
          <input type='file' name='upload' accept='.png,.gif,.jpg,.webp' required/>
          <input type='submit' name='submit' value='Upload Image'/>
        </form>";

        if (isset($_FILES['upload'])) 
        {
          $data["img"]=addslashes(file_get_contents($_FILES["upload"]["tmp_name"]));
          $this->db->insert('img',$data);

            $data=$this->db->query("select * from img");
            
              foreach ($data->result_array() as $row)
              {
                echo $row['id'];
?>
              <img src="data:image/jpg;charset=utf8;base64, <?php echo base64_encode($row['img']); ?> " height=200 width=200> </img>
<?php
              }
        }      
    }  
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何使用 PHP 和 SQL 将数据库中的值存储为 php 中的变量? - How am I supposed to store a value from my database as a variable in php using PHP and SQL? 从android应用程序获取数据并使用php将其存储在sql数据库中 - get data from android app and store it in sql database using php 如何在Codelgniter中上传.lrc文件? - How to upload an .lrc file in Codelgniter? 如何使用codelgniter更新数据库中的记录? - How to update the record in DB using codelgniter? 如何使用PHP从SQL数据库下载文件? - How to download file from SQL database using PHP? 如何使用php将图像或文件存储在文件夹中以及数据库中的该路径,但是图像或文件不是来自html的发布功能 - how to store image or file in a folder and that path in Database using php but image or file is not coming from post functionality of html 如何使用PHP从MYSQL数据库获取列类型并将其存储在变量中 - How to get column type from a MYSQL database using PHP and store that in a variable 如何使用PHP获取已选中复选框的名称并将其存储在数据库中 - How to get the name of checked checkboxes and store them in a database with using php 如何使用PHP在MySQL数据库中存储上载的swf文件? - How to store an uploaded swf file in a MySQL database using PHP? 如何使用php将输入数据存储到文本文件并从文本文件获取数据? - How to store input data using php into text file and get data from text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM