Welcome Guest, Not a member yet? Register   Sign In
Simple upload problem
#11

[eluser]alectrash[/eluser]
how do u rename file when its uploaded I followed the code in this tutorial n got it working but when i upload file i want to rename it? is this possible.

In my controller I have this
Code:
//load upload model
    $this->load->model('upload_model');
    
    $this->upload_model->avatar_upload('avatar_photo')

an in view file i have

Code:
<input type="file" name="avatar_photo" size="20" />

and in my model I have this

Code:
function avatar_upload()
{
      //handle profile pic uploads
      $directory = "/var/www/html/public/frontend/images/avatars";
      $config['upload_path'] = $directory;
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size']    = '100';
      $config['max_width']  = '1024';
      $config['max_height']  = '768';
      
      $this->load->library('upload', $config);

      if(!$this->upload->do_upload('avatar_photo'))
      {
        $error = array('error' => $this->upload->display_errors());
        return FALSE;
      }
      else
      {
        return TRUE;
      }
      
}

I want to put a line in the model somehwere that renames it if possible.
#12

[eluser]alectrash[/eluser]
its ok i figured it out.

changed my controller to this

Code:
function avatar_upload()
    {
      //handle profile pic uploads
      $directory = "/var/www/html/images/avatars";
      $config['upload_path'] = $directory;
      $config['allowed_types'] = 'jpg';
      $config['max_size']    = '100';
      $config['max_width']  = '1024';
      $config['max_height']  = '768';
      
      $this->load->library('upload', $config);

      if(!$this->upload->do_upload('avatar_photo'))
      {
        $error = array('error' => $this->upload->display_errors());
        //do a file error log maybe here?
        return FALSE;
      }
      else
      {
        $data = array('upload_data' => $this->upload->data());
        
        $oldfilename = "/var/www/html/images/avatars/" . $data['upload_data']['file_name'];
        $newfilename = "/var/www/html/images/avatars/av_" . $this->db_session->userdata('user_name') . ".jpg";
        
        rename($oldfilename, $newfilename);
        return TRUE;
      }
      
    }

hope people find this useful
#13

[eluser]vile[/eluser]
here u can use this:

$_FILES['userfile']['name'] = "anyname";
#14

[eluser]calingrim[/eluser]
@alectrash: I'm using the same thing to upload some pics but i need to rename them and it won't work.

Code:
else
      {
        $data = array('upload_data' => $this->upload->data());
        
        $oldfilename = "/path/to/file/" . $data['upload_data']['file_name'];
        $newfilename = "/path/to/file/" . $the_new_name . ".jpg";
        
        rename($oldfilename, $newfilename);
        return TRUE;
      }

And all I get is:
Code:
Message: rename(path/to/file/pic_name.jpg,) [function.rename]: No such file or directory
Message: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/ci_app/system/libraries/Exceptions.php:164)

Anybody got any idea?
Thanks in advance.

LE: Ok, I got it! the probles was that i was using realpath() and the function isn't adding a / and the end.




Theme © iAndrew 2016 - Forum software by © MyBB