tags:

views:

1187

answers:

6

Hi All, I'm working on an iPhone app that will upload images to a web server. I was wondering if anyone had any tips on how to generate unique names for each image file that gets uploaded. I'm sure there are a million ways to do this, but if anyone has any suggestions I'd really appreciate it! Thanks.

+2  A: 

The simplest solution (assuming you're storing these in a database) is to have an auto-increment field in the database, and use that to rename the file as it's uploaded. That way you'll end up with image00000001.jpg, image00000002.jpg, etc.

Mark Bessey
+2  A: 

you could create a GUID and save the image as that name ..

Thanks

Sandyx
This will work quite well for what I want to do. Here is a link with more info: http://stackoverflow.com/questions/427180/how-to-create-a-guid-uuid-using-the-iphone-sdk
Jeff
A: 

You could use the unix timestamp. This would allow you to update a record with a new file while still keeping the same id, instead of having to create a new record each time a file is changed. Some like:

 $uploadData = pathinfo($_FILES['file']['tmp_name']);
 move_uploaded_file($_FILES['file']['tmp_name'], time() . '.' . $uploadData['extension']);

I'd recommend a lot more checking to ensure the file is what you are looking for, such as mime type/extension checking, max size, and ensure the file is an uploaded file using is_uploaded_file().

Darryl Hein
+1  A: 

The simplest would be to convert the current time into a string and use that as a name. will always be unique :)

Or if you have a private key in your database, use it with a generic string to generate a unique name for each image.

lostInTransit
A: 

how do you upload files from an iphone? via php

A: 

You may want to consider using the device id of the generating phone to insure uniqueness across the universe of iPhones.

Jack Cox