CodeIgniter Forums
Use avatar from file or url - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Use avatar from file or url (/thread-25252.html)



Use avatar from file or url - El Forum - 12-05-2009

[eluser]Sein Kraft[/eluser]
Hi everyone. I've a simple file uploader wich set the avatar for the user but i want also add an input for set the avatar from url. The question is vallidate and use the url if the user havent selected a file but for that i should remove the avatar callback.

More than a problem is a performance question I think. What I should do?

This my code:

Code:
function _set_avatar(){
    
        $this->form_validation->set_rules('avatar_file', 'Avatar', 'callback__avatar_check');
        $this->form_validation->set_rules('avatar_url', 'Avatar', 'trim|min_length[5]');
            
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        if ($this->form_validation->run() == FALSE)
        {            
            $this->load->view('users/profile');
        }
        else
        {
            //Agregar aviso de error durante la subida del avatar.        
            $config_avatar['upload_path'] = './temp/';
            $config_avatar['allowed_types'] = 'gif|jpg|png';
            $config_avatar['overwrite'] = TRUE;
            $config_avatar['max_size']    = '100';
            $config_avatar['max_width']  = '100';
            $config_avatar['max_height']  = '100';
            
            $this->load->library('upload', $config_avatar);
        
            if (!$this->upload->do_upload('avatar_file'))
            {
                //I suppose that the code for the url should be here
            }    
            else
            {
                $data = array('upload_data' => $this->upload->data());
                $upload_data = $this->upload->data();
                
                $this->load->model('mUsers','', TRUE);
                $users = $this->mUsers->get_user('id', $this->session->userdata('id'));
                
                $temp_file = './temp/'.$upload_data['file_name'];
                $new_file = './accounts/'.$users->user.'/avatar'.$upload_data['file_ext'];
                
                //We delete the olda avatar to create the new one.
                unlink($new_file);
                rename($temp_file , $new_file);
                                
                $user_data['id'] = $users->id;
                $user_data['avatar'] = $this->mConfig->get_config('subdomain_accounts').$users->user.'/avatar'.$upload_data['file_ext'];
                
                $this->mUsers->set_avatar($user_data);
                
                redirect('users/profile');
            }
        }
        
        $this->load->view('footer');
    }
    
    
    
    /*
    - Check if the field of the avatar is not empty.
    */
    function _avatar_check(){
        if($_FILES['avatar_file']['error'] == 4){
            $this->form_validation->set_message('_avatar_check','File to use as avatar is required.');
            return FALSE;
        }
        else{
            return TRUE;
        }
    }



Use avatar from file or url - El Forum - 12-07-2009

[eluser]Sein Kraft[/eluser]
Ok...so no one have an idea?


Use avatar from file or url - El Forum - 12-07-2009

[eluser]jedd[/eluser]
[quote author="Sein Kraft" date="1260081822"]
I've a simple file uploader wich set the avatar for the user but i want also add an input for set the avatar from url.
[/quote]

I'm going to ignore your upload process and code, because uploading files is covered in detail elsewhere, and I don't think (?) this is the root of the question you're asking. I think you're asking how you can allow for a URL for an avatar or a file that the user has uploaded.

Is this right? If so, read on.

How I'd do it is .. have a flag in the user table that denotes the preference on local image or URL. Have space in the user table for the URL to reside. Have a directory somewhere that contains avatars, with the name of the file being the user.id or user.name - ie. unique to that user.

A function that says 'get url for user X's avatar' - takes a single parameter of user.id, and returns a string (url). Internally the function checks for the user table flag that denotes local .v. remote URL, if the latter returns it. If the former it creates a url based on the local web path / file system.

I'm not sure what performance issues you're expecting - the above is fairly streamlined, I think.


Use avatar from file or url - El Forum - 12-07-2009

[eluser]Sein Kraft[/eluser]
Not...I'm asking how detect and use a simple avatar uploader.

In the form the user has a file input and an common input text. I need to check if the user has uploaded a file or if the user has ussed the other input to set an external url for the avatar.

Thats my question. And yes, I use a table to save the ur of the users avatar, thas not the problem.


Use avatar from file or url - El Forum - 12-07-2009

[eluser]jedd[/eluser]
Okay then.

Can you turn this:

[quote author="Sein Kraft" date="1260081822"]
The question is vallidate and use the url if the user havent selected a file but for that i should remove the avatar callback.
[/quote]
.. into a question then?

And maybe explain what performance concerns you have?

It seems to me that you'll have a POST that contains either a populated URL field, or an unpopulated URL field. I don't see any complexity in checking if the URL field is populated.


Use avatar from file or url - El Forum - 12-07-2009

[eluser]Sein Kraft[/eluser]
Sorry, english is my second language.

Well but for thath i will remove the file input callback right? Becasue if the user don't sent a file but set an url then the submission fails right?


Use avatar from file or url - El Forum - 12-07-2009

[eluser]jedd[/eluser]
Don't panic - your English is better than any other language I've ever tried to learn. I think I must be having a dumb day, that's all.

I think either your callback has to somehow cope with checking both the url and the avatar file upload field, or you might be able to just set up two different rule groups, and do a quick check up front on whether the url is populated, and if so, run that rule group - otherwise (obviously) the other.

This isn't something I've done, so I'm just guessing here - hopefully someone else will pop in with their experience on this.


Use avatar from file or url - El Forum - 12-13-2009

[eluser]Sein Kraft[/eluser]
Thanks jedd for the answer Smile