gpt4 book ai didi

php上传文件唯一名称问题

转载 作者:行者123 更新时间:2023-11-29 04:49:33 25 4
gpt4 key购买 nike

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

所以当它上传时,所有文件都将以其各自的文件名进入上传目录。比如: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);
}
?>

最佳答案

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

关于php上传文件唯一名称问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13450627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com