greenday Posted September 27, 2007 Share Posted September 27, 2007 Is there a way to change the file name when uploading filenames to a db to prevent over writting of duplicate names? Thinking of either adding microtime to the file name or something? Here is my code: $uploaddir = "../img/listings-photos/"; foreach($_FILES["pic"]["name"] as $key => $current){ $uploadfile = $uploaddir . basename($current); if (!move_uploaded_file($_FILES["pic"]["tmp_name"][$key], $uploadfile)) { echo "{$current}"; } } $pl = mysql_query("SELECT * FROM `listtable` ORDER BY placeid DESC Limit 1;") or die(mysql_error()); while($rowp = mysql_fetch_array( $pl )) { // Print out the contents of each row into a table $plid = $rowp['placeid']; } //Writes the information to the database $address=$HTTP_POST_VARS['address']; $default = 'noimage.gif'; mysql_query("UPDATE `listtable` SET `mainpic` = '".( empty($_FILES['pic']['name'][0]) ? $default : $_FILES['pic']['name'][0] )."', `pic2` = '".( empty($_FILES['pic']['name'][1]) ? $default : $_FILES['pic']['name'][1] )."', `pic3` = '".( empty($_FILES['pic']['name'][2]) ? $default : $_FILES['pic']['name'][2] )."', `pic4` = '".( empty($_FILES['pic']['name'][3]) ? $default : $_FILES['pic']['name'][3] )."', `pic5` = '".( empty($_FILES['pic']['name'][4]) ? $default : $_FILES['pic']['name'][4] )."', `pic6` = '".( empty($_FILES['pic']['name'][5]) ? $default : $_FILES['pic']['name'][5] )."', `pic7` = '".( empty($_FILES['pic']['name'][6]) ? $default : $_FILES['pic']['name'][6] )."', `pic8` = '".( empty($_FILES['pic']['name'][7]) ? $default : $_FILES['pic']['name'][7] )."', `pic9` = '".( empty($_FILES['pic']['name'][8]) ? $default : $_FILES['pic']['name'][8] )."', `pic10` = '".( empty($_FILES['pic']['name'][9]) ? $default : $_FILES['pic']['name'][9] )."', `pic11` = '".( empty($_FILES['pic']['name'][10]) ? $default : $_FILES['pic']['name'][10] )."', `pic12` = '".( empty($_FILES['pic']['name'][11]) ? $default : $_FILES['pic']['name'][11] )."', `pic13` = '".( empty($_FILES['pic']['name'][12]) ? $default : $_FILES['pic']['name'][12] )."', `pic14` = '".( empty($_FILES['pic']['name'][13]) ? $default : $_FILES['pic']['name'][13] )."', `pic15` = '".( empty($_FILES['pic']['name'][14]) ? $default : $_FILES['pic']['name'][14] )."', `pic16` = '".( empty($_FILES['pic']['name'][15]) ? $default : $_FILES['pic']['name'][15] )."', `pic17` = '".( empty($_FILES['pic']['name'][16]) ? $default : $_FILES['pic']['name'][16] )."', `pic18` = '".( empty($_FILES['pic']['name'][17]) ? $default : $_FILES['pic']['name'][17] )."', `pic19` = '".( empty($_FILES['pic']['name'][18]) ? $default : $_FILES['pic']['name'][18] )."', `pic20` = '".( empty($_FILES['pic']['name'][19]) ? $default : $_FILES['pic']['name'][19] )."' WHERE `placeid` = '$plid'"); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/70841-change-file-name-on-upload-to-db/ Share on other sites More sharing options...
greenday Posted September 27, 2007 Author Share Posted September 27, 2007 help please! Quote Link to comment https://forums.phpfreaks.com/topic/70841-change-file-name-on-upload-to-db/#findComment-356748 Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 sure, append microtime or something. md5 the user's IP address, anything... Quote Link to comment https://forums.phpfreaks.com/topic/70841-change-file-name-on-upload-to-db/#findComment-356750 Share on other sites More sharing options...
greenday Posted September 27, 2007 Author Share Posted September 27, 2007 thanks BlueSkyIS, i am having difficulty in knowing where to put to microtime or md5 so that it will use the name for the db and as the file name. Please could you show me in my code?? Much appreciated if you could. Quote Link to comment https://forums.phpfreaks.com/topic/70841-change-file-name-on-upload-to-db/#findComment-356762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.