【发布时间】:2019-01-14 23:48:22
【问题描述】:
我正在自学 codeigniter,所以我使用了图像上传类,将名称存储在 SQL 中,并将图像存储在服务器的文件夹中。当您从服务器调用名称时,它将从文件中显示。
所以效果很好,但我对覆盖图像很好奇,所以我检查了关于上传文件的 codeignter 文档,它们有一个 encrypt_file 文件功能。这样就可以解决覆盖问题,因为我现在使用它,所以存储在我的数据库中的文件名是原始文件,而存储在服务器上我的文件夹中的图像具有加密的文件名,所以对于照片不显示。
我浏览了 codeigniter 的文档,尝试加载另一个文件名变量等等。我也尝试过每次都初始化配置。
库上传的控制器是自动的。
if($this->form_validation->run()=== FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
else{
//Upload image
$config['upload_path'] = 'assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE; //TURN ON
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error'=>$this->upload->display_errors());
$post_image = 'noimage.jpg';
}else {
$data = array('upload_data' =>$this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Post_Model->create_post($post_image);
redirect('http://localhost/CI/');
}
}
型号
public function create_post($post_image){
$slug = url_title($this->input->post('title'));
$data=array('title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'category_id' => $this->input->post('category_id'),
'post_image' =>$post_image
);
表格
<img src="assets/images/posts/<?php echo $post['post_image'];?>">
【问题讨论】:
-
你的目标是什么?上传相同的图像,但使用不同的名称?您是否将图像路径存储在数据库中以便稍后将其与例如新闻文章?
-
@Vickel 我希望能够存储图像而不会有覆盖相同图像的风险。我希望能够以博客文章或用户个人资料照片的形式检索文件。
标签: php codeigniter