Shibbir
Shibbir

Reputation: 21

Php uploaded file unique name issue

Well, I've a html Upload form which process by .php file (uploader.php). It's a multiple file uploaded code.

So when it's upload, all files are go to upload directory with their individual file name. Like: abc.jpg, abc2.jpg etc.

But If someone upload a file with same name then it's ovrwrite the existing file. So that I need a unique name for the uploaded file. Is there any way to rename the file with unique random name. Like: 12545454.jpge, 234324324.jpge, 323453253.jpg etc..

Php Code:

<?php
if (isset($_POST['Submit'])) {
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory


/**
 * we get a $_FILES['images'] array ,
 * we procee this array while iterating with simple for loop
 * you can check this array by print_r($_FILES['images']);
 */
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
    $number_of_file_fields++;
    if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
        $number_of_uploaded_files++;
        $uploaded_files[] = $_FILES['images']['name'][$i];
        if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . 
$_FILES['images']['name'][$i])) {
            $number_of_moved_files++;
        }

    }

}
echo "Number of File fields created $number_of_file_fields.<br/> ";
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
}
?>  

Upvotes: 1

Views: 1540

Answers (3)

Vikas Umrao
Vikas Umrao

Reputation: 2615

You can concat time(); and session_id() or uniqid() as suggested by wormhit is also very good idea against name of each file.

Use The below code:

<?php
$target_path = "images/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
//$upload_directory = ''.uniqid().'_'.dirname(__file__) . '/uploaded/'; //set upload directory

$uploaded_file="".$upload_directory ."/".uniqid()."_".$_FILES['images']['name'][$i]."";

if (move_uploaded_file($_FILES['images']['tmp_name'][$i],$uploaded_file)) {
            $number_of_moved_files++;
        }
?>


<?php
if (isset($_POST['Submit'])) {
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory


/**
 * we get a $_FILES['images'] array ,
 * we procee this array while iterating with simple for loop
 * you can check this array by print_r($_FILES['images']);
 */
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
    $number_of_file_fields++;
    if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not

        $uploaded_file="".$upload_directory ."".uniqid()."_".$_FILES['images']['name'][$i]."";

        $number_of_uploaded_files++;
        $uploaded_files[] = $_FILES['images']['name'][$i];
        if (move_uploaded_file($_FILES['images']['tmp_name'][$i],$uploaded_file)) {
            $number_of_moved_files++;
        }

    }

}
echo "Number of File fields created $number_of_file_fields.<br/> ";
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
}
?> 

Upvotes: 1

wormhit
wormhit

Reputation: 3827

Use uniqid() . It is made for this.

Upvotes: 3

Hmmm
Hmmm

Reputation: 1764

You can use this

 $ext = pathinfo($file_name, PATHINFO_EXTENSION);
 $newfilename = substr( sha1(uniqid (rand())), 0, 10). '.'.$ext;

Upvotes: 0

Related Questions