Search code examples
imagecodeigniterhyperlinkmove

Move Image to another Path from a clicked link; Codeigniter


The idea is to move an image selected by the current user to another path that's also in the database. Here is the Controller:

public function makeProfile()  {             
         if (isset($_GET['makeProfile']));           
         $data['user_id']= $this->session->userdata['user_id'];                      
                 $fileName = ('uploads/'.$fileName);              
    if($this->images_model->makeProfile($data, $fileName)) {            
         echo "Success";             
                     $msg = 'File successfully moved to profile';
        redirect(site_url().'main/');             
         }else{         
         echo "Failure";

The Model:

Public function makeProfile ($data, $fileName) {
return $this->db->get_where('images', ['user_id' => $this->session->userdata('user_id')])->row();
move_uploaded_file('uploads/'.$fileName, 'uploads/profile/'.$fileName);
if($this->db->affected_rows() == '1') {
return TRUE;
}
return FALSE;
   }

The VIEW:

<a class="profile" href="<?= base_url('main/makeProfile') ?>">Make Profile</a>

I’m probably going about this all wrong, but hoping someone here might steer me in the right direction. Thanks in advance for any and all input!


Solution

  • Function makeProfile in the model has an issue. You are getting the record and then returning it back. The code below is not executing

    Public function makeProfile ($data, $fileName) {
       $fileName= $this->db->get_where('images', ['user_id' => $this->session->userdata('user_id')])->row()->image;
    
        // Will copy foo/test.php to bar/test.php
       // overwritting it if necessary
       copy('uploads/'.$fileName, 'uploads/profile/'.$fileName));
       if($this->db->affected_rows() == '1') {
          return TRUE;
       }
       return FALSE;
    }
    

    Quoting a couple of relevant sentences from its manual page :

    Makes a copy of the file source to dest.

    If the destination file already exists, it will be overwritten.