Aromefraise
Aromefraise

Reputation: 45

Rename a file if same already exists

I'm trying to upload a file and rename it if it already exists. The way I want i to do is that when det same file uploads the name just adds 1, then 2, then 3, and so on.

Example: If file "file" exists, the new file should be "file1", then the next one "file2".

I've seen some examples on the net, but nothing that I could see fit to my code (noob)

This is my code now:

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

Can somebody help me where I can put in code that solves this problem? Big thank you!

Upvotes: 4

Views: 11712

Answers (3)

Avnish kumar Singh
Avnish kumar Singh

Reputation: 11

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

Upvotes: 1

SomeoneYouDontKnow
SomeoneYouDontKnow

Reputation: 1309

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

} 

But don't forget to secure your script (eg see comment of Marc B above) and maybe you could optimize some more ;-)

Upvotes: 8

Uxonith
Uxonith

Reputation: 1622

so if folder exists:

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

check if file exists

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

and then if it does,

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

So in the end you change your code to:

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

Upvotes: 1

Related Questions