Welcome Guest, Not a member yet? Register   Sign In
Upload multiple images, store img paths & info to db
#1

[eluser]Vasi[/eluser]
Hi guys,

I know there are a lot of tutorials on these subject, but due to the fact that I'm a newbie I having a lot of trouble putting this together. So this is I am trying to do:

- store the info from a form to db
- upload and create thumbnails for multiple images (3-5)
- store the image paths to db also

So far I only managed to upload & store to db just one image, but not resize it. I will attach my files because they are to big to paste.

I would really appreciate it, thank you guys!
#2

[eluser]Narkboy[/eluser]
First - don't look at this as one thing. It's 5 things:

1) Recieve form data
2) Store that into a database
3) Upload and store an image
4) Duplicate and resize the image
5) Store an image path in a database.

Work through each one seperatly. As you've said, there are many tutorials on this and you really need to work through this step-by-step to understand whats going on. If you got given the complete working code, you'd have a working script and no idea what it was doing. Not a greaat idea!

Build the product up piece by piece; recieve the form data, add it to an array. Store the image (just 1 to start with) and add the file name and location to the db arrray. Perform the copy & resize to create the thumbnail, and add those details to the db array. Add the array to the database. Done

Now go back and work out if you want batch uploads. Work out how to handle that. Easy! With that done, start adding in useful / sensible bits - max file size, file type verification, random file renames, etc to make sure that the script is safe for use.

GL! Smile
#3

[eluser]Vasi[/eluser]
Thanks a lot Narkboy, but here is the thing I can't seem to solve:

In my controller I a have this two functions: insert_to_db() and do_upload() - the catch is I need to insert into the database path of the image which is in the do_upload() function, how can I pass variables through the functions which are in the same controller?
#4

[eluser]Narkboy[/eluser]
Ok - it this situation I would simply drive around the entire thing.

Anything to do with your database, move into a model. Any method that accesses the database to send or retrieve data should be in that model.

The do_upload() method could also go in a model (different one) but I would normally leave it in the controller unless it's being used in more than one place. Lazy, but defensible!

So your controller goes:

Code:
function do_upload()
{
    // upload the image, store path and file name etc as $info

    // now call the model and get it to drop to the db:
    $this->my_db_model->insert_to_db($info);

    // Now error check the db insert to ensure that it worked.

    // Now show the user something.
}

That way, the task is done logically, in order, and by the correct parts of the MVC framework. You can resue the insert_to_db function for uploads in other places as well.

Smile




Theme © iAndrew 2016 - Forum software by © MyBB