[英]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.