如何上传图像并将其保存到 Codeigniter 中的数据库?
How to upload an image and save it to database in Codeigniter?
我知道这个问题已经被问过数百万次了,但是所有的问题对于初学者来说都有点太难了。
所以我正在寻找的是如何创建一个简单的按钮来上传图像并获取其 full_path 并将其保存在表单的隐藏输入中,因此当我单击提交时,表单将插入带有其他输入的路径,因此图像将被上传到目录。
这是一个简单的项目,一个基本的博客页面,我需要在其中保存以下输入:
1. 图片
2.标题
3. Body
我唯一做不到的是图像部分。
查看代码如下:
<h1>Add New Post</h1>
<br />
<?=form_open('admin/add_post');?>
<p>
<input type="text" name="title" />
</p>
<input hidden="hidden" name="date" value="<?php echo date('d-m-y');?>"/>
<p>
<textarea id="textarea" name="body" rows="10"></textarea>
</p>
<p>
<input type="submit" value="Submit Post" />
</p>
</form>
点击提交后的控制器如下:
// Adds new posts to the database table
function add_post()
{
$this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
$this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');
if ($this->form_validation->run() == TRUE) {
// INSERTING GOES HERE..
$this->db->insert('entries', $_POST);
redirect('admin/posts');
}
else {
redirect('admin/add');
}
}
所以我需要有人告诉我如何编写上传功能来在同一页面和同一表单上上传图片,并将其路径保存到数据库中。
我可以让它保存路径,但我想先获取路径并将其添加到隐藏输入中的相同表单,EX:
我真的需要你的帮助,如果你对如何帮助我有任何想法,请post你的回答。
提前致谢。
使用 CodeIgniter 实现这一点非常简单。
我将使用版本 3.1.0 参考:
查看:
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
控制器:
public function do_upload() {
$fileData = array();
// File upload script
$this->upload->initialize(array(
'upload_path' => './uploads/',
'overwrite' => false,
'max_filename' => 300,
'encrypt_name' => true,
'remove_spaces' => true,
'allowed_types' => 'gif|jpg|png',
'max_size' => 100,
'xss_clean' => true,
));
$this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
$this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');
if ($this->form_validation->run() == TRUE) {
if ($this->upload->do_upload('userfile')) {
$data = $this->upload->data(); // Get the file data
$fileData[] = $data; // It's an array with many data
// Interate throught the data to work with them
foreach ($fileData as $file) {
$file_data = $file;
}
var_dump($file_data);
$this->db->insert('entries', array(
// So you can work with the values, like:
'title' => $this->input->post('title', true), // TRUE is XSS protection
'body' => $this->input->post('body', true),
'file_name' => $file_data['file_name'],
'file_ext' => $file_data['file_ext'],
));
$this->session->set_flashdata('success', 'Form submitted successfully');
redirect('admin/add');
} else {
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('admin/add');
}
} else {
$this->session->set_flashdata('error', validation_errors());
redirect('admin/add');
}
}
您可以使用 var_dump 查看可用数据,或查看文档(在底部)
https://www.codeigniter.com/userguide3/libraries/file_uploading.html
我知道这个问题已经被问过数百万次了,但是所有的问题对于初学者来说都有点太难了。
所以我正在寻找的是如何创建一个简单的按钮来上传图像并获取其 full_path 并将其保存在表单的隐藏输入中,因此当我单击提交时,表单将插入带有其他输入的路径,因此图像将被上传到目录。
这是一个简单的项目,一个基本的博客页面,我需要在其中保存以下输入: 1. 图片 2.标题 3. Body
我唯一做不到的是图像部分。
查看代码如下:
<h1>Add New Post</h1>
<br />
<?=form_open('admin/add_post');?>
<p>
<input type="text" name="title" />
</p>
<input hidden="hidden" name="date" value="<?php echo date('d-m-y');?>"/>
<p>
<textarea id="textarea" name="body" rows="10"></textarea>
</p>
<p>
<input type="submit" value="Submit Post" />
</p>
</form>
点击提交后的控制器如下:
// Adds new posts to the database table
function add_post()
{
$this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
$this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');
if ($this->form_validation->run() == TRUE) {
// INSERTING GOES HERE..
$this->db->insert('entries', $_POST);
redirect('admin/posts');
}
else {
redirect('admin/add');
}
}
所以我需要有人告诉我如何编写上传功能来在同一页面和同一表单上上传图片,并将其路径保存到数据库中。
我可以让它保存路径,但我想先获取路径并将其添加到隐藏输入中的相同表单,EX:
我真的需要你的帮助,如果你对如何帮助我有任何想法,请post你的回答。
提前致谢。
使用 CodeIgniter 实现这一点非常简单。
我将使用版本 3.1.0 参考:
查看:
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
控制器:
public function do_upload() {
$fileData = array();
// File upload script
$this->upload->initialize(array(
'upload_path' => './uploads/',
'overwrite' => false,
'max_filename' => 300,
'encrypt_name' => true,
'remove_spaces' => true,
'allowed_types' => 'gif|jpg|png',
'max_size' => 100,
'xss_clean' => true,
));
$this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
$this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');
if ($this->form_validation->run() == TRUE) {
if ($this->upload->do_upload('userfile')) {
$data = $this->upload->data(); // Get the file data
$fileData[] = $data; // It's an array with many data
// Interate throught the data to work with them
foreach ($fileData as $file) {
$file_data = $file;
}
var_dump($file_data);
$this->db->insert('entries', array(
// So you can work with the values, like:
'title' => $this->input->post('title', true), // TRUE is XSS protection
'body' => $this->input->post('body', true),
'file_name' => $file_data['file_name'],
'file_ext' => $file_data['file_ext'],
));
$this->session->set_flashdata('success', 'Form submitted successfully');
redirect('admin/add');
} else {
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('admin/add');
}
} else {
$this->session->set_flashdata('error', validation_errors());
redirect('admin/add');
}
}
您可以使用 var_dump 查看可用数据,或查看文档(在底部)
https://www.codeigniter.com/userguide3/libraries/file_uploading.html