Best practice.

Hello guys,
I'm trying to name an image with the entry id when I first add the product.
What I mean is I don't have an id yet when first doing the upload and the database entry.

The only way I came out with is:

1- Validations
2- Upload
3- Database entry
4- Rename file with the id from step #3 rename('image/img.gif', 'image/'.$id.'.gif');

It this the best way.


How about this.

1. Upload image to tmp public location (save location into flash data). This means if the form validation fails the user doesn't have to re-upload.
2. Form Validation
3. Check Upload was a success
4. Database Entry, return unique ID
5. Move file to final location.

I agree with adamp1, save the file somewhere so you can keep track of it. Finalize it when done. Prevent the user from having to upload twice.

One thing to look out for: you need to remove the file when it ends up not being used. For instance if the user decides not to continue with the process of adding.
This could be as simple as checking if there are no files older than a day in the upload folder, and deleting them if there are any.

Another thing: it is not always necessary or even useful to have the pictures named by the id of the item. What if you want to upload more than 1 file per item? I usually name the picture uniquely by getting a uniqid() and then store the id with the item. This also solves the problem of not knowing the item's id. You don't need to know it for storing the picture.

What I was doing, I was making codeigniter name the file something like _image.jpg in my upload folder then get the clients id and rename that file to logo_1(id).jpg, this way I don't have to store the name in the database I just ref the file 'logo_'.$row->id and that's it. Also the file is not going to upload until it pass all the form validations.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.