【问题标题】:i am new in php i need to upload file with random name我是 php 新手,我需要上传随机名称的文件
【发布时间】:2019-05-16 06:26:48
【问题描述】:

我是 php 新手,我需要上传带有随机名称的文件并分配给文件并将该文件存储为随机名称以上传文件夹并将该随机名称存储到 mysql 数据库中。

  $pic_file1 = $this->input->post('pic_file');

    $pic_file1 = str_replace( "\\", '/', $pic_file1);
    $filename = time().basename($pic_file1);


            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 1000;
            //$config['encrypt_name'] = TRUE;
            // $config['overwrite'] = FALSE; 
            $config['file_name'] =  $filename;          

            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('pic_file'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());
                   // print_r($data);                       

            }

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:
In your code just uncomment $config['encrypt_name'] = TRUE; 
then automatically your file name store in random name formate or jsut copy below code

 $pic_file1 = $this->input->post('pic_file');

    $pic_file1 = str_replace( "\\", '/', $pic_file1);
    $filename = time().basename($pic_file1);


            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 1000;
            $config['encrypt_name'] = TRUE;
            // $config['overwrite'] = FALSE; 
            $config['file_name'] =  $filename;          

            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('pic_file'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());
                   // print_r($data);                       

            }



【讨论】:

  • 随机名称存储在数据库中,但在实际文件位置,名称仅替换为进入数据库的字符串
  • print_r($data);然后遵循数组规则并获取已经上传到您的文件夹中的实际文件名,然后运行插入查询并相应地传递文件。
  • 您也可以分享您的插入查询,然后我会告诉您要传递的实际参数并分享 print_r($data);价值
  • $data = array( 'setpassword'=>$setpassword, 'conpassword'=>$conpassword, 'city'=>$city, 'products'=>$products, 'bank_type'=> $bank_type, 'bank_name'=>$bank_name, 'dsa_code'=>$dsa_code, 'pic_file'=>$filename ); $this->db->where('id',$dataId); $this->db->update('tbl_reg_dsa',$data);
  • 分享你上传的文件的 repose 数据 print_r($data);
【解决方案2】:

以上代码与任何框架相关。如果您是 php 新手,请尝试使用下面提到的简单代码。

$target_dir = "/var/www/html/uploads/"; // this is base path
$imageFileType = strtolower(pathinfo(basename($_FILES["pic_file"]["name"]),PATHINFO_EXTENSION));
$filename = time().$imageFileType; //save this file name to database $filename
$target_file = $target_dir.$filename
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["pic_file"]["tmp_name"]);
    if($check !== false) {
          if (move_uploaded_file($_FILES["pic_file"]["tmp_name"], $target_file)) {
             echo "The file ". basename( $_FILES["pic_file"]["name"]). " has been uploaded on : ".$target_file;
         } else {
             echo "Sorry, there was an error uploading your file.";
         }
    } else {
        echo "File is not an image.";        
    }
}

如果您还需要 HTML,请告诉我我会提供。

【讨论】:

    【解决方案3】:

    $pic_file1 = $this->input->post('pic_file');

            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 1000;
            //$config['encrypt_name'] = TRUE;
            // $config['overwrite'] = FALSE; 
            $config['file_name'] =  time();          
    
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
    
            if ( ! $this->upload->do_upload('pic_file'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());
                   // print_r($data);                       
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2017-03-31
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      相关资源
      最近更新 更多