重命名文件(如果已存在)

时间:2014-05-30 21:36:03

标签: php file-upload filenames file-rename file-exists

我尝试上传文件并重命名(如果已存在)。 我希望我做的方式是,当同一文件上传时,名称只添加1,然后是2,然后是3,依此类推。

示例:如果文件"文件"存在,新文件应该是" 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);   } }

有人可以帮我解决这个问题吗? 非常感谢你!

3 个答案:

答案 0 :(得分:5)

$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");
    }

    if(!is_dir("filer/".$id."/")) {
        mkdir("filer/".$id); 
    }

    $rawBaseName = pathinfo($fname, PATHINFO_FILENAME );
    $extension = pathinfo($fname, PATHINFO_EXTENSION );
    $counter = 0;
    while(file_exists("filer/".$id."/".$fname)) {
        $fname = $rawBaseName . $counter . '.' . $extension;
        $counter++;
    };

    move_uploaded_file($_FILES['dok']['tmp_name'],"filer/".$id."/".$fname);  

} 

但是不要忘记保护你的剧本(例如看上面Marc B的评论),也许你可以优化一些; - )

答案 1 :(得分:1)

所以如果文件夹存在:

file_exists("filer/".$id."/")

检查文件是否存在

file_exists("filer/".$id."/".$fname)

然后如果是的话,

$fname = $fname . "(1)" // or some appending string

最后,您将代码更改为:

// Check if user have his own catalogue
if (file_exists("filer/".$id."/")) {
    while (file_exists("filer/".$id."/".$fname)) // Now a while loop
        $fname = "copy-" . $fname; // Prepending "copy-" to avoid breaking extensions

    // 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 {

答案 2 :(得分:1)

<form action="test.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>

<?php
$id = $_SESSION['id'];
$fname = $_FILES['fileToUpload']['name'];
 // Checking filesize
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "uploads/".$id."/".$fname)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}else {
    echo "Sorry, there was an error uploading your file.";
}
// Check file size$
if ($_FILES['fileToUpload']['size']>1048576) {
    die("The file is too big. Max size is 1MB");
}
if(!is_dir("uploads/".$id."/")) {
    mkdir("uploads/".$id); 
}

$rawBaseName = pathinfo($fname, PATHINFO_FILENAME );
$extension = pathinfo($fname, PATHINFO_EXTENSION );
$counter = 0;
while(file_exists("uploads/".$id."/".$fname)) {
    $fname = $rawBaseName . $counter . '.' . $extension;
    $counter++;
};

move_uploaded_file($_FILES['fileToUpload']        ['tmp_name'],"uploads/".$id."/".$fname);  
?>