Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filemanger overwriting files (solution) #2639

Closed
tarranjones opened this issue Feb 2, 2015 · 0 comments
Closed

filemanger overwriting files (solution) #2639

tarranjones opened this issue Feb 2, 2015 · 0 comments

Comments

@tarranjones
Copy link

Filemanger currently overwrites files with the same name.
this change will allow the user to choose between overwriting the file or auto increment the file name if it exists.

filemanager.tpl

var overwrite = typeof overwrite != 'undefined' ? overwrite : '';
$.ajax({
    url: 'index.php?route=common/filemanager/upload&token=<?php echo $token; ?>&directory=<?php echo $directory; ?>'+overwrite,
    ........
    success: function(json) {
       overwrite = ' ';
        if (json['error']) {
            if(json['error'] =='exist'){
                overwrite = '&overwrite='+confirm("File exists, Replace old file ? ");
                $('#button-upload').trigger('click');                    
            } else {
                 alert(json['error']);
            }
        }
            ......

Filemanger.php

if(!isset($this->request->get['overwrite']) && file_exists($directory.'/' . $filename)){
    //file exists and user hasn't been asked
    $json['error'] = 'exist';
} else {

    if(isset($this->request->get['overwrite']) && !$this->request->get['overwrite']) {
        //user has been asked(which means file exists ) and doesnt want to overwrite
         $info = pathinfo($filename);
         $i = 0; 
        // rename until file doesnt exist 
         while(file_exists($directory.'/' . $filename)){ 
             $filename = $info['filename']  . '-' . $i++ . $info['extension'];
         }
    }
    move_uploaded_file($this->request->files['file']['tmp_name'], $directory . '/' . $filename);

/* drag/drop upload is currently missing, actually any form of instant upload(no filemanger). 
This provides some better json feedback so it is possible to create js to upload files without the filemanger . */


    if(is_file($directory . '/' . $filename)) {
            $this->load->model('tool/image');
            $json = array(
                'thumb' => $this->model_tool_image->resize(utf8_substr($directory . '/' . $filename, utf8_strlen(DIR_IMAGE)), 100, 100),
                'path' => utf8_substr($directory . '/' . $filename, utf8_strlen(DIR_IMAGE)),
            );
        }
    $json['success'] = $this->language->get('text_uploaded');
}   

This could go further and allow the user to rename the file using prompt()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants