PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] File upload and save the file name with combination of date and ID of user (Codeigniter)

 February 19, 2022     codeigniter     No comments   

Issue

My target is when i save the file upload on my database, the filename should be the combination of userID and datetime. So for example: The original file name is C231.jpg. When i saved it on my database, the filename should be 0112021443 (01 < User id) (12021 = Date) (443 = time). Any help will be appreciated. Thank you in advance.

View:

<form method="post"  action="<?php echo site_url('ewallet/cashins')?>" enctype="multipart/form-data">

                     <div class="input-group">
                      
                      <input type="number" class="form-control" id="box" name="amount1" min="100"  required>
                  <div class="input-group-append">    
                     <button type="button" id="doClear" class="btn btn-danger btn-flat"><i class="fas fa-times-circle"></i></button>
                  </div>
                   </div>
                   <br>
             
                     
                      <br><br>
                        <label for="exampleInputEmail1"><i class="fas fa-image mr-2"></i>Upload Receipt Picture</label>
                        
                       <input type="file" name="fileName" class="form-control btn-sm" min="50" id="fileName">
                    <br>
           <input type="submit" class="amount btn btn-success btn-sm float-right text-bold" name="save" id="insert" value="CONFIRM">
                    
                </form>

Controller:

**public function cashins() {
        $this->ewallets->cashpasok();
        redirect("ewallet/cashin");
    }**

Model:

function cashpasok() {
     
        $ref= $this->session->userdata('userID') + time ();
        $data = array (
            'refNumber' => 'CI' . $ref,
            'userID' => $this->session->userdata('uid'),
            'username' => $this->session->userdata('username'), 
            'amount' => $this->input->post('amount1'),
            'status' => "pending",
            'transtype' => "cash_in",
            'remarks' => "",
            'fileName' => $this->upload(),
            
            
        );
        $this->db->insert('cash_in', $data);
    }

function upload(){
        $ref= $this->session->userdata('userID') + time ();
        $pic=array(
            
            'upload_path'=>'public/assets/uploads',
            'allowed_types'=>'gif|jpg|png',
            'max_size'=>4000,
            'max_width'=>10024,
            'max_height'=>10024,
        );
        $this->load->library("upload",$pic);
        $this->upload->initialize($pic);
       
        if($this->upload->do_upload('fileName')){
            $fb=$this->upload->data();
            $fd=$fb['file_name'];
            return $fd;
        }
        else{
            
            $data =  $this->input->post('fileName');
            return $data;
        }
    }

Solution

try this,

$file_name = $_FILES['fileName']['name'];
$Extension = pathinfo($file_name, PATHINFO_EXTENSION);
$ref= $this->session->userdata('userID').time ();//changes
$fileName = $ref . '.' . $Extension;

$pic=array(
      'upload_path'=>'public/assets/uploads',
      'allowed_types'=>'gif|jpg|png',
      'max_size'=>4000,
      'max_width'=>10024,
      'max_height'=>10024,
      'file_name' = $fileName;//changes to give custom file name
        );


Answered By - M.Hemant
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing