Reputation: 5526
I'm coding a php script to get an uploaded picture, move it in a folder (let's say "/img") renaming it with a unique name, create a thumbnail with the same name in a subfolder ("/img/small"), and insert a record for the picture in my database where the picture's name is the primary key.
Basically I'm obtaining the unique name from the database itself, after an INSERT INTO, with mysql_insert_id. This means that I'm moving the picture and creating the thumbnails after the database insertion, so if something goes wrong (e.g.: invalid image format, not enough space, etc.) I should perform a "DELETE" from the database, pseudocode:
perform "INSERT INTO ..."
name = inserted_id
if (!move_file(temp, "/img/" + name)) perform "DELETE WHERE id = name"
if (!create_thumbnail(temp, "/img/small" + name)) perform "DELETE WHERE id = name"
I'd like to know if some of you faced the same problem and came out with a more rational and elegant solution.
Upvotes: 1
Views: 986
Reputation: 360692
Use transactions instead. The basic procedure would be:
Upvotes: 1