我正在尝试上传文件并重命名它,如果它已经存在.我希望我做的方式是,当相同的文件上传时,名称只添加1,然后是2,然后是3,依此类推.
示例:如果存在文件"file",则新文件应为"file1",然后是下一个"file2".
我在网上看到过一些例子,但我认为没有什么能适合我的代码(noob)
这是我现在的代码:
$id = $_SESSION['id'];
$fname = $_FILES['dok']['name'];
if ($_FILES['dok']['name'] !=""){
// Checking filetype
if($_FILES['dok']['type']!="application/pdf") {die("You can only upload PDF files");}
// Checking filesize
if ($_FILES['dok']['size']>1048576) {die("The file is too big. Max size is 1MB");}
// Check if user have his own catalogue
if (file_exists("filer/".$id."/")) {
// Moving the file to users catalogue
move_uploaded_file($_FILES['dok']['tmp_name'],"filer/".$id."/".$fname);}
//If user don't have his own catalogue
else {
// Creates new catalogue then move the file in place
mkdir("filer/".$id);
move_uploaded_file($_FILES['dok']['tmp_name'],"filer/".$id."/".$fname); } }
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我在哪里可以放入解决这个问题的代码吗?非常感谢!