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!

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.