Welcome Guest, Not a member yet? Register   Sign In
Renaming file names when file uploading
#1

[eluser]shankar ganesh[/eluser]
The below code for uploading and renaming files, how to rename files when uploading files and not after uploading.

Code:
$this->load->library('upload');

            /*Uploading Image and Banner files*/
            /*================================*/

            foreach($_FILES as $key => $value)
            {
                if(!empty($_FILES[$key]['name']))
                {
                    $config=array();
                    if($_FILES[$key]['type']=="image/jpeg" || $_FILES['image']['type']=="image/gif")
                    {
                        $config['upload_path'] = 'upload/images/';
                        $config['allowed_types'] = 'gif|jpg';
                    }
                    $this->upload->initialize($config);
                    if ($this->upload->do_upload($key))
                    {
                        $data = array('upload_data' => $this->upload->data());
                        $file = $data['upload_data']['file_name'];
                        $newfilename=date('Ydmhis').rand(5,100)."__".$file;
                        rename($config['upload_path'] . $file, $config['upload_path'] .$newfilename);
                    }    
                }
            }
#2

[eluser]Unknown[/eluser]
Refer to the file uploading class in the user guide.

You can specify the new name of the file in the config.

Code:
$config['upload_path'] = 'upload/images/';
$config['allowed_types'] = 'gif|jpg';
$config['file_name'] = 'yourfilenamehere';
#3

[eluser]shankar ganesh[/eluser]
Its fine, thanks
#4

[eluser]Unknown[/eluser]
thanks for the share, i do have the same problemSmileSmileSmile
#5

[eluser]SPeed_FANat1c[/eluser]
I have one question about this

$config['file_name'] = 'yourfilenamehere';

I rename it to let's say photo.jpg (I have to add extention, becuase othervise it does not allow to upload). But when png file is uploaded, it also is renamed to 'photo.jpg'. Not a big problem, I can still use those files, but is it good practice to do so?




Theme © iAndrew 2016 - Forum software by © MyBB