-2

I am new in php i need to upload file with random name assigning to the file and store that file with random name to upload folder and store that random name into mysql database.

  $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);                       

            }
  • Possible duplicate of [php file upload guide](https://stackoverflow.com/questions/3696803/php-file-upload-guide) – Jan May 16 '19 at 06:30

3 Answers3

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);                       

            }



Sarfaraz
  • 146
  • 8
  • random name stored in database but at actual file location the name is replace only with string which goes into database – user3636036 May 16 '19 at 07:05
  • print_r($data); then follow array rule and get actual file name which was already uploaded in your folder and then run insert query and pass file accordingly. – Sarfaraz May 16 '19 at 07:07
  • You may also share your insert query then i will tell u actual parameter to passs and also share print_r($data); value – Sarfaraz May 16 '19 at 07:08
  • $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); – user3636036 May 16 '19 at 07:12
  • share your file uploaded repose data print_r($data); – Sarfaraz May 16 '19 at 07:14
  • Array ( [upload_data] => Array ( [file_name] => dd9dcb622980ff281f67bc5d3582fbcd.png [file_type] => image/png [file_path] => C:/xampp/htdocs/paisabhai/uploads/ [full_path] => C:/xampp/htdocs/paisabhai/uploads/dd9dcb622980ff281f67bc5d3582fbcd.png [raw_name] => dd9dcb622980ff281f67bc5d3582fbcd [orig_name] => 1557991124.png [client_name] => jpbhstamp.png [file_ext] => .png [file_size] => 81.99 [is_image] => 1 [image_width] => 300 [image_height] => 300 [image_type] => png [image_size_str] => width="300" height="300" ) ) – user3636036 May 16 '19 at 07:19
  • your file name $filename =$data['upload_data']['file_name']; , now you can add this code below to this line $data = array('upload_data' => $this->upload->data()); – Sarfaraz May 16 '19 at 07:22
  • in my data_insert how to get filename – user3636036 May 16 '19 at 11:35
  • upvoting because `$config['encrypt_name'] = TRUE;` is the way to go. The file will get renamed to a random string and `$this->upload->data()` will provide both the original and new names so you can store both as references in your table if you so desire – Javier Larroulet May 16 '19 at 13:55
0

Above code is related any framework. if you are new in php then try with below mentioned simple code.

$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.";        
    }
}

If you need HTML also please let me know i will provide.

Pankaj Chauhan
  • 1,623
  • 14
  • 12
0

$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);                       

        }
jd_ginoya
  • 16
  • 3