如何使用codeigniter上载库在数据库中插入文件名
在本例中,可以使用codeigniter上载库将图像上载到文件中 不过,我想在数据库中发送文件名。谁能给我举个例子吗?谢谢如何使用codeigniter上载库在数据库中插入文件名,codeigniter,Codeigniter,在本例中,可以使用codeigniter上载库将图像上载到文件中 不过,我想在数据库中发送文件名。谁能给我举个例子吗?谢谢 <?php class Upload extends Controller { function Upload() { parent::Controller(); $this->load->helper(array('form', 'url')); } function index() { $this->load-
<?php
class Upload extends Controller {
function Upload()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
或者我们还有其他选择吗?您可以使用
data()
这样的方法获得它:
$upload_data = $this->upload->data();
echo $upload_data['file_name'];
请注意,您也可以获取其他上传数据,只需检查您拥有的:
print_r($upload_data);
创建一个数组,其中包含您需要上传到数据库中的任何内容…然后将其插入数据库中,并将类似的内容放在“其他”下:
//create array to load to database
$insert_data = array(
'name' => $image_data['file_name'],
'path' => $image_data['full_path'],
'thumb_path'=> $image_data['file_path'] . 'thumbs/'. $image_data['file_name'],
'tag' => $tag
);
$this->db->insert('photos', $insert_data);//load array to database
重要提示:数组的索引必须与表中的列同名
还可以尝试使用您的模型,它将使您的代码更易于维护、阅读等
祝你好运 您可以这样使用:
$data = array('upload' => $this->upload->data());
echo $data['upload']['file_name'];
我需要写模型函数吗?比如将文件信息发送到数据库()并在控制器中写入$this->db->insert('some_table')?任何代码帮助都会很好。ThanksI做了$this->db->insert('upload_file',$data['upload_data']);但它正在发送所有的数据数组,我只想其中的四个字段进入数据库。我该怎么做?我知道这是4年前的事了,但它确实帮了我的忙,谢谢!!非常感谢,这与我想要的非常接近,但是我收到了以下错误:消息:未定义变量:image\u数据和列“file\u name”不能为null插入
上传文件
(文件名,完整路径,原名)值(null,null,null)。。。。。。。。。我的show create table是create tableupload_file
(id
int(11)NOT NULL,file_name
varchar(80)NOT NULL,full_path
orig_name
varchar(80)NOT NULL)ENGINE=MyISAM DEFAULT CHARSET=latin1try并将这行代码放在前面:$image\u data=$this->upload->data();
$config['upload_path'] = './assets/uploads/'; // Upload Path will be here
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);
if ($this->upload->do_upload('upload')) {
$file_data = $this->upload->data();
$file_name= $file_data['file_name']; //get file name of your uploaded file from here..
}
else {
echo "Error";
}
$data = array(
'image_name' => $file_data['file_name'], //insert your image name from here..
);
$this->add_model->image_upload($data); //that will go to the model function..
}