Don't allow overwrite

Added by Tony Hunter 12 months ago

Hi Sirs,
I've been searching for a way to NOT allow a document to be overwritten on upload.

The idea is to when uploading a file with the same name and extension i increment the name

test.txt

next upload would be

test(01).txt or something similar.

Any idea where i can find and edit this function?

thanks

tony


Replies (8)

RE: Don't allow overwrite - Added by Troex Nevelin 12 months ago

Currently there is no such option, it will be implemented in next version 2.x.

For 1.x you can modify the _upload() method in elFinder.class.php

RE: Don't allow overwrite - Added by Dmitry Levashov 12 months ago

In version 2.x its already implemented :)
Thanks for good idea!

RE: Don't allow overwrite - Added by Luciano Barbosa 12 months ago

Is there any chance i can take a glimpse at 2.x class code? :-)

RE: Don't allow overwrite - Added by Dmitry Levashov 12 months ago

No. 2.x has been rewritten from scratch, so it's code is not compatible with 1.x branch. Wait a little >_<

RE: Don't allow overwrite - Added by Luciano Barbosa 12 months ago

spent a couple of hours and there we go...

Add in connector.php

Best Regards

Luciano and Tony

private function _upload()
    {

        if (empty($_POST['current']) 
        || false == ($dir = $this->_findDir(trim($_POST['current'])))) {
            return $this->_result['error'] = 'Invalid parameters';
        } 
        if (!$this->_isAllowed($dir, 'write')) {
            return $this->_result['error'] = 'Access denied';
        }
        if (empty($_FILES['upload']))
        {
            return $this->_result['error'] = 'No file to upload';
        }

        $this->_logContext['upload'] = array();
        $this->_result['select'] = array();
        $total = 0;
        for ($i=0, $s = count($_FILES['upload']['name']); $i < $s; $i++) { 
            if (!empty($_FILES['upload']['name'][$i])) {
                $total++;
                $this->_logContext['upload'][] = $_FILES['upload']['name'][$i];
                if ($_FILES['upload']['error'][$i] > 0) {
                    $error = 'Unable to upload file';
                    switch ($_FILES['upload']['error'][$i]) {
                        case UPLOAD_ERR_INI_SIZE:
                        case UPLOAD_ERR_FORM_SIZE:
                            $error = 'File exceeds the maximum allowed filesize';
                            break;
                        case UPLOAD_ERR_EXTENSION:
                            $error = 'Not allowed file type';
                            break;
                    }
                    $this->_errorData($_FILES['upload']['name'][$i], $error);
                } elseif (false == ($name = $this->_checkName($_FILES['upload']['name'][$i]))) {
                    $this->_errorData($_FILES['upload']['name'][$i], 'Invalid name');
                } elseif (!$this->_isUploadAllow($_FILES['upload']['name'][$i], $_FILES['upload']['tmp_name'][$i])) {
                    $this->_errorData($_FILES['upload']['name'][$i], 'Not allowed file type');                    
                } else {

                    if (file_exists($dir.DIRECTORY_SEPARATOR.$_FILES['upload']['name'][$i])) 
                    {
                        $arquivonovo=$dir.DIRECTORY_SEPARATOR.$_FILES['upload']['name'][$i];
                        $extencao = substr($arquivonovo,-4);
                        $ContaCaracter = strlen($arquivonovo);
                        $nomearquivo = substr($arquivonovo,($ContaCaracter*-1),($ContaCaracter-4));
                        $datagravacao = date('YmdHis');
                        $arquivoantigo = $nomearquivo.'_'.$datagravacao.$extencao;

                        rename($arquivonovo,$arquivoantigo);

                        $file = $dir.DIRECTORY_SEPARATOR.$_FILES['upload']['name'][$i];
                    }
                    else
                    {
                        $file = $dir.DIRECTORY_SEPARATOR.$_FILES['upload']['name'][$i];
                    }

                    if (!@move_uploaded_file($_FILES['upload']['tmp_name'][$i], $file)) {
                        $this->_errorData($_FILES['upload']['name'][$i], 'Unable to save uploaded file');    
                    } else {
                        @chmod($file, $this->_options['fileMode']);
                        $this->_result['select'][] = $this->_hash($file);

                    }
                }
            }
        }

        $errCnt = !empty($this->_result['errorData']) ? count($this->_result['errorData']) : 0;

        if ($errCnt == $total) {
            $this->_result['error'] = 'Unable to upload files';
        } else {
            if ($errCnt>0) {
                $this->_result['error'] = 'Some files was not uploaded';    
            }
            $this->_content($dir);
        }

    }

RE: Don't allow overwrite - Added by Dmitry Levashov 12 months ago

Nice work!
To create unique name you can use _uniqueName() method :)

(1-8/8)