PHP File Name

Soldato
Joined
30 Nov 2005
Posts
3,084
Location
London
Developing an Upload in PHP and want to change the uploaded file name to the record ID in the MySQL database.

So basicaly when the user uploads, the file name is changed to become the record ID. This means that there will never be the same file name, because the ID is an Int and auto_increment.

Not sure the best way to do this is.

Any ideas?

Cheers all.
 
Last edited:
when you use move_uploaded_file or whatever it is, just go (off the top of my head)

Code:
move_uploaded_file($_FILES[temp],sprintf('%s.doc',$mysql_id));

you'll obviously need to include the path there, too but you get the idea
 
Thanks, I saw that but because I'm uploading directly into the MySQL database (I know it's wrong to do it this way, so don't ask:rolleyes:), does this not apply?
 
Last edited:
ah right, you're putting the file straight into the db?

I'd have a table called files that looked a bit like this:

id - int
file - blob

then I'd base64encode the file into the table (you don't have to, but I would). I think the filename is redundant as the id will reference it for you. I didn't realise you were putting the file directly in the db so I'm not 100% sure on how to do this but that should work.
 
Wait, because I'm storing it directly in a database would it matter if two file names where the same? Because the file table has auto_increment ID's and also stores the User who uploaded it's ID, it would already be unique?

Gosh I'm confused. I think I don't need to change the file name after all?
 
you won't need to change the file name because it'll be irrelevant - the db's id field will tell you where the file is.
 
Back
Top Bottom