1

好吧,我有一个由.php 文件(uploader.php)处理的html 上传表单。这是一个多文件上传的代码。

因此,当它上传时,所有文件都会以其各自的文件名进入上传目录。比如:abc.jpg、abc2.jpg 等。

但是如果有人上传了一个同名的文件,那么它就会覆盖现有文件。所以我需要上传文件的唯一名称。有没有办法用唯一的随机名称重命名文件。喜欢:12545454.jpge、234324324.jpge、323453253.jpg等。

php代码:

<?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);
}
?>  
4

3 回答 3

3

使用uniqid()。它是为此而生的。

于 2012-11-19T09:25:49.230 回答
1

您可以按照 wormhit 的建议 concattime();session_id()oruniqid()对每个文件的名称也是非常好的主意。

使用以下代码:

<?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);
}
?> 
于 2012-11-19T09:20:30.277 回答
0

你可以用这个

 $ext = pathinfo($file_name, PATHINFO_EXTENSION);
 $newfilename = substr( sha1(uniqid (rand())), 0, 10). '.'.$ext;
于 2012-11-19T09:25:24.527 回答