【问题标题】:Image Upload to database reference original name图片上传到数据库参考原始名称
【发布时间】:2019-02-17 23:39:05
【问题描述】:

我正在开发一个 codeigniter 项目。在图像上传配置中,我将其加密以允许唯一的文件名,以避免覆盖和名称加倍,并提高一般安全性。

所以在上传时它将加密图像文件名,并将加密的名称存储在数据库中,同时将图像保存在我的资产文件夹中。但由于某种原因,它似乎根本没有加密图像名称。几乎就像它完全忽略了$config 选项而只是上传图像。

我还尝试了一个回调函数来避免空白上传,并且似乎也被忽略了并且仍然允许发布。

如果有人可以给小费。请。

控制器

//Callback validation
$this->form_validation->set_rules('userfile','Photo','callback_photo_check');

    if($this->form_validation->run() === FALSE){

      $this->load->view('templates/header');
      $this->load->view('posts/create', $data);
      $this->load->view('templates/footer');

    } else {

       if($this->form_validation->run()==TRUE){

      $config['upload_path'] = 'assets/images/posts';
      $config['allowed_types']        = 'gif|jpg|jpeg';
      $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());

              $this->load->view('templates/header');
              $this->load->view('posts/create', $errors);
              $this->load->view('templates/footer');

      }else {
        $this->post_model->create_post($this->upload->data('full_path'),$this->input->post());
      }

    }

    $this->session->set_flashdata('post_created','Your Post has been submitted');

    redirect('posts');

    }
  }


public function photo_check(){

if(empty($_FILES['userfile'])){
  $this->form_validation->set_message('photo_check', 'need a image');
                       return FALSE;
}

else{
  return TRUE;
}

}

型号

public function create_post($path,$post){

$data = array(

'about'=> $this->input->post('Description'),
'image' => $path,

);

return $this->db->insert('posts',$data);

【问题讨论】:

标签: php codeigniter codeigniter-3


【解决方案1】:

我之前也遇到过同样的问题,然后我决定给它们(文件)一个唯一的名称,我所做的是: • 我分配了一个空变量,它将保存要修改的文件名/路径数据,并将其命名为$info_name。 • 每次文件名在现有位置重复时,它都会添加一个独特的扩展名,例如时间(秒、日期等)。

这是我的示例代码:

public function new_info($data,$photo){
    extract($data);
    $info_name = "";
    $directory = "C:/xampp/htdocs/Parent folder/child folder/grand child folder/";
    $extension= array("jpeg","jpg","png","gif");
    $file_name=$photo["form_name"]["name"];
    $file_tmp=$photo["form_name"]["tmp_name"];
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);
    if(in_array($ext,$extension)){
        if(!file_exists($directory.$file_name)){
            move_uploaded_file($file_tmp=$photo["form_name"]["tmp_name"],$directory.$file_name);
            $info_name = $file_name;
        }
        else{
            $filename=basename($file_name,$ext);
            $newFileName=$filename.time().".".$ext;
            move_uploaded_file($file_tmp=$photo["form_name"]["tmp_name"],$directory.$newFileName);
            $info_name = $newFileName;
        }
    }

   // then your sql code here for example: 
   $data= array( 'user' => $_SESSION["user_id"],
                 'picture' => $info_name,
                );
    $this->db->insert('sys_post',$data);
  }

【讨论】:

    【解决方案2】:

    要加密上传的文件名,您必须按照以下步骤操作。

    1) 您必须加载加密库。

    您可以在有上传代码的特定页面上调用此库。

    // LOAD LIBRARIES
    $this->load->library('encryption');
    

    或者你也可以在$autoload['libraries'] = array('database','form_validation','encryption');autoload.php文件中加载它

    2) 现在您使用的是加密类,您必须在 config.php 文件中设置一个加密密钥。

    $config['encryption_key'] = 'your-own-encryption-key';
    

    有关加密的更多信息 => https://codeigniter.com/user_guide/libraries/encryption.html

    3) 最后,在您的上传代码中$config['encrypt_name'] = TRUE;

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 2014-03-29
      • 2013-12-25
      • 2018-06-27
      相关资源
      最近更新 更多