Jump to content

Ensure uploaded file has a unique filename


squigs

Recommended Posts

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!

 

 

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;
}
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.