|
Posted by Nadim Attari on 03/31/05 10:44
If you are storing the name of the uploaded image in a table (mySQL DB),
then the row in which you gonna store this name must have a unique id
(auto_increment field) ... why don't you use this id as your image name.
here you'll NEVER have the same image name.
Example
You have an HTML form where surfer enter details + upload an image. On
submitting the form, the PHP script stores all the details + uploads the
image and put it in the right folder on the server.
Table myTable
id int(6) unsigned auto_increment
imagename varchar(15)
otherdetails varchar(255)
once you insert a row in the table, get the id of the row. Use this id to
name ur image.
steps:
1. mysql_query("insert into myTable (imagename, otherdetails) values ('',
'$otherdetails')");
2. get rowid - mysql_insert_id()
3. rename image to "id.ext"
4. update row in table: update myTable set imagename = 'id.ext' where id =
'id'
You may use this script to do step 3
http://www.alienworkers.com/misc/uploadImage.htm
Hope it helps...
Nadim Attari,
Alienworkers.com
> Hello,
>
> I got a bit frustrated with image upload stuff with different image name
> problems. So I created a system that gives the uploaded imaged a random
> numeric name between 1-10 000 000 and saves the file to a server folder
and
> the image name to mysql DB.
>
> Is there a so sort of a problem here that I am not thinking of? I only can
> imagine problem that the rand() gives the same value twice. But I cant see
> this as a major problem because there would be maybe not more than 1000
> uploaded pictures. So the chance is at worst something like 1:10 000 that
> same name is created to the image.
>
> Anyway if same name is created what's the best way to check that? I was
> thinking of putting the image name field in DB as a unique field. That
would
> do it? Right?
>
> Thanks again
> -Will
[Back to original message]
|