Jump to content
Sign in to follow this  
Chrisj

How to not overwrite an uploaded file name ?

Recommended Posts

I'm using dropzone js successfully to upload files from a web page.
However, if a file gets uploaded that has the same name as a file already in the destination folder, it will overwrite the existing folder file.

I tried to remedy this by adding the js script function as shown below, but it didn't work:

<div id="dropzone">
<form action="/uploadDrop.php" class="dropzone"></form>
</div>
 
<script type="text/javascript">
Dropzone.autoDiscover = false;
$(document).ready(function () {
    $(".dropzone").dropzone({
        renameFilename: function (filename) {
            return new Date().getTime() + '_' + filename;
        }
    });
});
</script>

Any help with this will be appreciated.

 

Share this post


Link to post
Share on other sites

The Javascript shouldn't be the one that sets the filename. Do it in uploadDrop.php. Whose code you did not post.

Share this post


Link to post
Share on other sites

Thanks for your message.

 

Here is the uploadDrop.php code:

<?php
$ds          = DIRECTORY_SEPARATOR;
$storeFolder = 'uploadDrop';
if (!empty($_FILES)) {
    $tempFile = $_FILES['file']['tmp_name'];
    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
    $targetFile =  $targetPath. $_FILES['file']['name'];
    move_uploaded_file($tempFile,$targetFile);
}
?>

Any additional help will be appreciated.

Share this post


Link to post
Share on other sites

That is not good. Anyone can upload any file to any directory on your server.

 

Here is a link that looks good to me. Ignore the Cloudinary stuff. Read and understand what it does, then adjust it to work with your site.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.