Image upload and database insert, correct way

4 messages Options
Embed this post
Permalink
Raavi Raaj

Image upload and database insert, correct way

Reply Threaded More More options
Print post
Permalink
Hi,
 
Wishing everyone a very happy, prosperous and fun-filled 2009.
--
 
I have an image upload form, where on successful upload...
1. I copy the image from the temp location, to its final location (after renaming)
2. Delete the original image from the temp folder
3. Insert the image information into the database
 
All works fine, but what i'd like to know is, if I am doing the steps in the right order (1,2,3).
One drawback of the above order is that I do not have the insert id while renaming the image (which i'd really like to have). But at the same time I am not sure if db insert should be done first and the image renamed/cleaned later.
 
All guidance is appreciated.
 
-R
 
P.S. I am on shared host, where there are (not very often, but they do happen) db issues :)
alan.bem

Re: Image upload and database insert, correct way

Reply Threaded More More options
Print post
Permalink
Well there is numerous of ways to do that.

I did some reconnaissance and have some advices:
1. Name your stored images with some unique hash (not database primary key) e.g. sha1(time() . $original_name. It will prevent auto download via bot-programs and original name collisions.
2. Use transaction during all upload logic.
try {
    $database->beginTransaction();
    // upload logic: move uploaded files, hash generating etc
    // database logic: inserting image data to database
    $database->commit();
catch(UploadException $e) {
    // just rollback and maybe some logging
    $database->rollback();
} catch(DatabaseException) {
    $database->rollback();
    // delete uploaded file as well, since db failed.
}

As you can see in my example step order doesn't really matter. Of course you can do that in procedural code without OOP (but I don't recommend that).
Raavi Raaj

Re: Image upload and database insert, correct way

Reply Threaded More More options
Print post
Permalink
Thanks. Great advice.
One question. Will the below code work with MYISAM?
 
-R

 
On 1/1/09, Alan Gabriel Bem <[hidden email]> wrote:

Well there is numerous of ways to do that.

I did some reconnaissance and have some advices:
1. Name your stored images with some unique hash (not database primary key)
e.g. sha1(time() . $original_name. It will prevent auto download via
bot-programs and original name collisions.
2. Use transaction during all upload logic.
try {
   $database->beginTransaction();
   // upload logic: move uploaded files, hash generating etc
   // database logic: inserting image data to database
   $database->commit();
catch(UploadException $e) {
   // just rollback and maybe some logging
   $database->rollback();
} catch(DatabaseException) {
   $database->rollback();
   // delete uploaded file as well, since db failed.
}

As you can see in my example step order doesn't really matter. Of course you
can do that in procedural code without OOP (but I don't recommend that).
--
View this message in context: http://www.nabble.com/Image-upload-and-database-insert%2C-correct-way-tp21239704p21242660.html
Sent from the Zend Core mailing list archive at Nabble.com.


Marco Pracucci-2

Re: Image upload and database insert, correct way

Reply Threaded More More options
Print post
Permalink
Hi,

> One question. Will the below code work with MYISAM?

MyISAM does not support transactions.

Marco Pracucci