squigs Posted August 28, 2012 Share Posted August 28, 2012 Hi I've created a form that accepts multiple attachments. It is looking/working better everyday with a little help from the folks at php freaks! In testing the script and using the same files over and over I realized that unique file names were not being assigned to the uploaded files. I am looking for some advice on how to handle this. I will post relevant code to give an example of the variables I have in use. foreach ($_FILES["uploaded_file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key]; $name_of_uploaded_file =($_FILES["uploaded_file"]["name"][$key]); $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_FILES["uploaded_file"]["size"][$key]; $temp_path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; //copy the temp. uploaded file to uploads folder if(is_uploaded_file($tmp_name)) { if(!copy($tmp_name,$temp_path_of_uploaded_file)) { $errors .= '\n error while copying the uploaded file'; } $path_of_uploaded_file[] = $temp_path_of_uploaded_file; } } } Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/267679-ensure-uploaded-file-has-a-unique-filename/ Share on other sites More sharing options...
.josh Posted August 28, 2012 Share Posted August 28, 2012 can just append a timestamp based hash as part of the filename Quote Link to comment https://forums.phpfreaks.com/topic/267679-ensure-uploaded-file-has-a-unique-filename/#findComment-1373083 Share on other sites More sharing options...
squigs Posted August 28, 2012 Author Share Posted August 28, 2012 can just append a timestamp based hash as part of the filename Good thinking! I should be able to accomplish that. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/267679-ensure-uploaded-file-has-a-unique-filename/#findComment-1373085 Share on other sites More sharing options...
squigs Posted August 28, 2012 Author Share Posted August 28, 2012 Done! (and it works too) I will note the changes in case it helps someone else.. foreach ($_FILES["uploaded_file"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["uploaded_file"]["tmp_name"][$key]; $name_of_uploaded_file =($_FILES["uploaded_file"]["name"][$key]); $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_FILES["uploaded_file"]["size"][$key]; $temp_path_of_uploaded_file = $upload_folder . $timestamp . $name_of_uploaded_file; //this is where I appended the timestamp //copy the temp. uploaded file to uploads folder if(is_uploaded_file($tmp_name)) { if(!copy($tmp_name,$temp_path_of_uploaded_file)) { $errors .= '\n error while copying the uploaded file'; } $path_of_uploaded_file[] = $temp_path_of_uploaded_file; } } } Quote Link to comment https://forums.phpfreaks.com/topic/267679-ensure-uploaded-file-has-a-unique-filename/#findComment-1373089 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.