代码点火器-如何将图像保存到数据库 [英] Code Igniter - How to save images into database
本文介绍了代码点火器-如何将图像保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用codeIgniter将文件上传到服务器中的文件夹中.现在我还想将数据保存到数据库中,我需要什么才能获取原始数据然后将其保存到数据库中?
I'm making use of codeIgniter to upload files into folders in the server. Now I also want to save the data into the database, what do I need in order to get the raw data and then save it into the database?
这是保存文件的模型:
<?php
class files extends ci_Model{
function saves($filename, $filedata, $post_id){
$this->db->query("INSERT INTO tbl_files SET file_data='$filedata', filename='$filename', postid='$post_id'");
}
}
?>
这是我从上传控制器调用它的方式:
Here's how I call it from the upload controller:
$filename = $data['upload_data']['file_name'];
$file_data = file_get_contents($data['upload_data']['file_name']);
$this->load->model('files');
$this->files->saves($filename, $file_data, 'ID1');
表结构:
- file_data(LONG BLOB)
- 文件名(VARCHAR)
推荐答案
我已经通过使用方便的mysql_real_escape_string()这样解决了这个问题:
I have solved the problem by using the handy mysql_real_escape_string() like this:
$filename = $data['upload_data']['file_name'];
$file_data = mysql_real_escape_string(file_get_contents($data['upload_data']['full_path']));
这篇关于代码点火器-如何将图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文