hi, could someone explain mysql_insert_id() to me?
I'm uploading files to an upload folder, but they just keep getting named the same thing, and writing over each other. here's a bit of my code:
$extension = explode('.',$_FILES['upload']['name']);
$uid = mysql_insert_id();
$filename = $uid.'.'. $extension[1];
couldn't I just use the original name ($_FILES['upload']['name']), i hear this is somehow a security issue.
mysql_insert_id()
Moderator: General Moderators
mysql_insert_id() simply gives you the index id of the last thing you inserted into a database.
Try something like:
Try something like:
Code: Select all
$filename = rand(1, 10000000).'.$extension[1];
while (file_exists($filename))
{
$filename = rand(1, 10000000).'.'.$extension[1];
}
Last edited by Grim... on Wed Aug 24, 2005 10:38 am, edited 1 time in total.
ok so in fact yes both of you do rock, you get an A+ in response time, 2 cookies and 4 gold stars (for what it's worth).
however, i tried both suggestions and feyd's returned a fatal error,
and Grim's returned a parse error. I feel like I should have been capable of figuring out the parse error, but I couldn't. so, what i tried to do was go back to my original code. i added a new column in my table smallint(4), auto_increment, and assigned it as the primary key, and now I'm getting an error that tells me there's a duplicate (0) key. I'm thinking this is because it's somehow not incrementing the key for each uploaded file.
however, i tried both suggestions and feyd's returned a fatal error,
and Grim's returned a parse error. I feel like I should have been capable of figuring out the parse error, but I couldn't. so, what i tried to do was go back to my original code. i added a new column in my table smallint(4), auto_increment, and assigned it as the primary key, and now I'm getting an error that tells me there's a duplicate (0) key. I'm thinking this is because it's somehow not incrementing the key for each uploaded file.
works ! here's the code: ( in case someone else has the same problem)
Code: Select all
$extension = explode('.',$_FILES['upload']['name']);
$filename = rand(1,1000000).'.'. $extension[1];
while (file_exists($filename)) {
$filename = rand(1,1000000).'.'.$extension[1];
}Code: Select all