Welcome Guest, Not a member yet? Register   Sign In
Image Uploader / thumbnail - controller question
#1

[eluser]deadfrog[/eluser]
Hi guys - I'm making an image uploader for my girlfriends website and if I don't get it done soon I'm for it, you know how it is Smile

Anyway, I have the uploader part working fine, and I can get my 'upload success' page to display the details of the image in an array.

What I need to do next is to use the image manipulation class to create a thumbnail 200px wide, and display it on the next screen.

First question (and I'm sure this is REALLY easy) is how do I manipulate the array in the controller so I can extract the image name (to pass to the thumbnail creator)? I can't get it to work and it seems so simple I'm sure I'm overthinking it. $this->upload->data() is the array I have after upload.

Second question, once uploaded, if I repeatedly refresh the screen it'll continue to post the form and upload more images. I guess this is linked to the first question as I can stop it from uploading if the file name is the same (or something), but is there a better way?

Thanks for your time, sorry if I'm being thick!
#2

[eluser]Seppo[/eluser]
About the first question, in the $this->upload->data() array you have a "file_name" index, and a "full_path" key, both with the upload file name, the first one without the absolute path and the second with it.

About the second, the usual thing is to redirect after the upload/image manipulation to another page where you display the thumbnail... For example, you have an image controller with an upload method... once you have done the upload/resizing you'll redirect the user to another method, for example "display" passing the image id in the URL. To redirect you can use the redirect function in the url helper.
#3

[eluser]deadfrog[/eluser]
Thanks for the reply seppo! I'm absolutely sure I'm just being a dullard here, but I can't seem to figure out how I get the specific information I want from the array '$this->upload->data()'. I am fairly new to PHP but getting there!

Thanks for the heads up on redirect, that's perfect and works a treat.
#4

[eluser]Seppo[/eluser]
Code:
$data = $this->upload->data();
echo 'file_name: ' . $data['file_name'];
echo "\n<br />";
echo 'full_path: ' . $data['full_path'];
#5

[eluser]deadfrog[/eluser]
Gah, I WAS being a dullard. I didn't think to assign the array to a variable first - hoorah, thanks muchly friend!
#6

[eluser]deadfrog[/eluser]
As Columbo would say, "Just one more thing".

I've implemented the upload > DB thing, which returns the image ID and I'm passing it in the URL as a redirect as you suggested. The end result being something like:

Code:
upload/choose/8

However, if I refresh this screen, the 8 changes to a 9 and so on forever. Is there a way of doing this so I don't have this issue?
#7

[eluser]Kemik[/eluser]
Sounds like it's creating more and more DB entries.

Try using flashdata. I.e. once uploaded set_flashdata('uploaded', $image_id); and check for the flashdata on page load. If it exists return the image using the image_id from the flashdata.

If you don't want to do that then you may have your redirect at the wrong place in the code. Post it up here and we'll take a look.
#8

[eluser]deadfrog[/eluser]
OK, here's my do_upload function. I'm not making use of the M bit of MVC, but I will address that at a later date.

Code:
function do_upload() {
        $config['upload_path'] = './uploads/';
        $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()) {
            $this->content['error'] = array('error' => $this->upload->display_errors());
            $this->load->view('upload/do_upload', $this->content);
        }    
        else {
            $data = $this->upload->data();
            $this->content['upload_data'] = $data;
            $this->load->database();
            $dbdata = array(
                        'name' => $data['file_name'],
                        );
            $this->db->insert('images', $dbdata);
            $image_id = $this->db->insert_id();
            redirect('upload/choose/'.$image_id, 'location');
            //$this->load->view('upload/'$image_id, $this->content);
        }
    }
#9

[eluser]deadfrog[/eluser]
Hm, something I did has fixed this. The code above may not contain an error, so apologies if I've wasted anyones time on this last question.

Thanks for your help though, it's now working splendidly.




Theme © iAndrew 2016 - Forum software by © MyBB