好吧,我有一个html上传表单,由.php文件(uploader.php)处理。这是一个多文件上传的代码。
因此,当它上传时,所有文件都会以其各自的文件名转到上传目录。喜欢:abc.jpg,abc2.jpg等。
但如果有人上传了一个同名文件,那么它就会对现有文件进行ovrwrite。所以我需要一个唯一的名称来上传文件。有没有办法用唯一的随机名称重命名该文件。喜欢: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);
}
?>
答案 0 :(得分:3)
使用uniqid()。它是为此而制造的。
答案 1 :(得分:1)
你可以根据蠕虫的建议联合time();
和session_id()
或uniqid()
对每个文件的名称也是一个好主意。
使用以下代码:
<?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);
}
?>
答案 2 :(得分:0)
您可以使用此
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$newfilename = substr( sha1(uniqid (rand())), 0, 10). '.'.$ext;