Welcome Guest, Not a member yet? Register   Sign In
Renaming image on upload?
#1

[eluser]ywftdg[/eluser]
I have been trouble trying to get this renaming stuff to work at all, but no go. I tryed the latest post on here, but this causes a big error saying I cannot modify headers, etc...no idea. Has anyone out there mastered this uploading and renaming process? I am trying to take whatever they upload, set it to 777 and then rename it to $id + [ext]. Do I need to upload the item, then run another if statement to check for the file, then run the thumbnail and make a copy to get a new name? Right now the thumbnail doesn't work either if I add it into the current configs.

My current code (minus any renaming because it isnt working):

Code:
if ($this->input->post('mypageupdate')) {        
            
            //     Designers Upload Folder
            $dir = './assets/images/'.$id.'/';
            
            //    Make Directory
            if (!is_dir($dir)) {
                $theupload_path = mkdir('./assets/images/'.$id.'/', 0777);
            }
            
            
            $config['upload_path'] = $dir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['remove_spaces']  = TRUE;
            $config['quality']  = '90';  
            $config['max_size'] = '1000000';
            $this->upload->initialize($config);
            
            if (!$this->upload->do_upload())    {
                $data['error'] = 'There was a problem with your upload!';
            
            } else {
                
                $data = array('upload_data' => $this->upload->data());
                $filepath = $data['upload_data']['file_name'];
                
                $data = array(
                $this->input->post('nickname'),
                $this->input->post('bio'),
                $this->input->post('web'),
                $filepath
                );
        
                $this->designer->update_mypage($id,$data);
                $this->session->set_flashdata('message', 'Mypage Updated!');
                
                redirect('designers/mypage');
                
            }
        }
#2

[eluser]codex[/eluser]
[quote author="ywftdg" date="1217793575"]I have been trouble trying to get this renaming stuff to work at all, but no go. I tryed the latest post on here, but this causes a big error saying I cannot modify headers, etc...no idea. Has anyone out there mastered this uploading and renaming process? I am trying to take whatever they upload, set it to 777 and then rename it to $id + [ext]. Do I need to upload the item, then run another if statement to check for the file, then run the thumbnail and make a copy to get a new name? Right now the thumbnail doesn't work either if I add it into the current configs.

My current code (minus any renaming because it isnt working):

Code:
if ($this->input->post('mypageupdate')) {        
            
            //     Designers Upload Folder
            $dir = './assets/images/'.$id.'/';
            
            //    Make Directory
            if (!is_dir($dir)) {
                $theupload_path = mkdir('./assets/images/'.$id.'/', 0777);
            }
            
            
            $config['upload_path'] = $dir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['remove_spaces']  = TRUE;
            $config['quality']  = '90';  
            $config['max_size'] = '1000000';
            $this->upload->initialize($config);
            
            if (!$this->upload->do_upload())    {
                $data['error'] = 'There was a problem with your upload!';
            
            } else {
                
                $data = array('upload_data' => $this->upload->data());
                $filepath = $data['upload_data']['file_name'];
                
                $data = array(
                $this->input->post('nickname'),
                $this->input->post('bio'),
                $this->input->post('web'),
                $filepath
                );
        
                $this->designer->update_mypage($id,$data);
                $this->session->set_flashdata('message', 'Mypage Updated!');
                
                redirect('designers/mypage');
                
            }
        }
[/quote]

I'm having no problems whatsoever renaming uploaded files. What's the code for this part:
$this->designer->update_mypage?

Just make sure you get the path to the uploaded image right, then do:

Code:
rename('/path/to/uploaded/'. $data['upload_data']['file_name'], DESTINATION);

Should work just fine.
#3

[eluser]ywftdg[/eluser]
$this->designer->update_mypage

is:
Code:
function update_mypage($id,$data)    {
        $data = array(
                   'dNickname' => $data[0],
                   'dBio' => $data[1],
                   'web' => $data[2],
                   'dBanner' => $data[3]
        );
        
        $this->db->where('designer.dDesignerId', $id);
        $this->db->update('designer', $data);
        
        
       }

The code you mentioned, where should that go in the process of all this?
#4

[eluser]codex[/eluser]
Try this:

Code:
if ($this->input->post('mypageupdate')) {        
            
            //     Designers Upload Folder
            $dir = './assets/images/';
            
            $config['upload_path'] = $dir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['remove_spaces']  = TRUE;
            $config['quality']  = '90';  
            $config['max_size'] = '1000000';
            $this->upload->initialize($config);
            
            if (!$this->upload->do_upload())    {
                $data['error'] = 'There was a problem with your upload!';
            
            } else {
                
                $data = array('upload_data' => $this->upload->data());
                $file = $data['upload_data']['file_name'];
                
                rename($dir . $file, $dir .'test.jpg');
                
            }
        }

If this works, go from there.
#5

[eluser]ywftdg[/eluser]
Edit//
Never-mind got it, it wrote the image 'test' . Now I guess my question is, if I put the thumbnail code and such into the config above, will it write the thumbnail during all this as well, or do I need to run the thumbnail now AFTER the file rename?
#6

[eluser]codex[/eluser]
[quote author="ywftdg" date="1217800650"]Edit//
Never-mind got it, it wrote the image 'test' . Now I guess my question is, if I put the thumbnail code and such into the config above, will it write the thumbnail during all this as well, or do I need to run the thumbnail now AFTER the file rename?[/quote]

As long as you try to make the thumbnail AFTER the file has uploaded it shouldn't matter. But what I'd do is upload the image, rename and put it in the right directory, create thumbnail from the image in the directory.

PS: Putting the thumbnail code in the config wouldn't make any sense since the above config is specifically for the uploading process, not for image manipulation. Unless you call $this->image_lib->resize() after upload.
#7

[eluser]ywftdg[/eluser]
Wonderful, got it all working, thanks Codex! I thought I would post the final code so maybe will help another newb like me in the future.

Code:
if (!$this->upload->do_upload())    {
                $data['error'] = 'There was a problem with your upload!'; // If something goes wrong
            
            } else { // all is well, lets go!
                
                $data = array('upload_data' => $this->upload->data()); // Get the file from the form userfile information
                $file = $data['upload_data']['file_name']; // set the file variable
                
                $filenew = rename($dir . $file, $dir . $id.'.jpg'); //basic php rename call, rename my upload now that upload finished
                
                $data = array( // this array is all the other values from my form fields
                $this->input->post('nickname'),
                $this->input->post('bio'),
                $this->input->post('web'),
                $filenew
                );
                
                $configB['image_library'] = 'gd2'; // this code begins the thumbnail making process, from user guide
                $configB['source_image']    = $dir . $id.'.jpg'; // I am using $id for image name, which is my users id, comes form the session
                $configB['create_thumb'] = TRUE;
                $configB['maintain_ratio'] = TRUE;
                $configB['width'] = 57;
                $configB['height'] = 40;
                $this->load->library('image_lib', $configB);
                $this->image_lib->resize();
                    
                $this->designer->update_mypage($id,$data); // run my model which saves all this to the database, image name also ($filenew)
                $this->session->set_flashdata('message', 'Mypage Updated!'); // sets a session to flash data on page reload to say this has completed
                
                redirect('designers/mypage'); // redirect to the page with the session data ready
                
            }
        }
#8

[eluser]codex[/eluser]
[quote author="ywftdg" date="1217839654"]Wonderful, got it all working, thanks Codex! I thought I would post the final code so maybe will help another newb like me in the future.[/quote]

No problem. Glad to help!

PS: I like your work.




Theme © iAndrew 2016 - Forum software by © MyBB