protected function _deleteOldUpload(Entity $entity, $field = false, $newFile = false, array $options = [])
    {
        if ($field === false || $newFile === false) {
            return true;
        }

        $fileInfo = pathinfo($entity->$field);
        $newFileInfo = pathinfo($newFile);

        if (isset($options['defaultFile']) && (is_bool($options['defaultFile']) || is_string($options['defaultFile']))) {
            $this->_defaultFile = $options['defaultFile'];
        }

        if ($fileInfo['basename'] == $newFileInfo['basename'] ||
            $fileInfo['basename'] == pathinfo($this->_defaultFile)['basename']) {
            return true;
        }

        if ($this->_prefix) {
            $entity->$field = str_replace($this->_prefix, "", $entity->$field);
        }

        $file = new File($this->_config['root'] . $entity->$field, false);

        if ($file->exists()) {
            $file->delete();
            return true;
        }

        return false;
    }
protected function _deleteOldUpload(Entity $entity, $field = false, $newFile = false, array $options = [])
    {
        if ($field === false || $newFile === false) {
            return true;
        }

        $fileInfo = pathinfo($entity->$field);
        $newFileInfo = pathinfo($newFile);

        if (isset($options['defaultFile']) && (is_bool($options['defaultFile']) || is_string($options['defaultFile']))) {
            $this->_defaultFile = $options['defaultFile'];
        }

        if ($fileInfo['basename'] == $newFileInfo['basename'] ||
            $fileInfo['basename'] == pathinfo($this->_defaultFile)['basename']) {
            return true;
        }

        if ($this->_prefix) {
            $entity->$field = str_replace($this->_prefix, "", $entity->$field);
        }

        $file = new File($this->_config['root'] . $entity->$field, false);

        if ($file->exists()) {
            $file->delete();
            return true;
        }

        return false;
    }
protected function _deleteOldUpload ( Entity $ entity , $ field = false , $ newFile = false , array $ options = [ ] ) { if ( $ field === false || $ newFile === false ) { return true ; } $ fileInfo = pathinfo ( $ entity -> $ field ) ; $ newFileInfo = pathinfo ( $ newFile ) ; if ( isset ( $ options [ 'defaultFile' ] ) && ( is_bool ( $ options [ 'defaultFile' ] ) || is_string ( $ options [ 'defaultFile' ] ) ) ) { $ this -> _defaultFile = $ options [ 'defaultFile' ] ; } if ( $ fileInfo [ 'basename' ] == $ newFileInfo [ 'basename' ] || $ fileInfo [ 'basename' ] == pathinfo ( $ this -> _defaultFile ) [ 'basename' ] ) { return true ; } if ( $ this -> _prefix ) { $ entity -> $ field = str_replace ( $ this -> _prefix , "" , $ entity -> $ field ) ; } $ file = new File ( $ this -> _config [ 'root' ] . $ entity -> $ field , false ) ; if ( $ file -> exists ( ) ) { $ file -> delete ( ) ; return true ; } return false ; }
Delete the old upload file before to save the new file.

We can not just rely on the copy file with the overwrite, because if you use
an identifier like :md5 (Who use a different name for each file), the copy
function will not delete the old file.

@param \Cake\ORM\Entity $entity  The entity that is going to be saved.
@param bool|string      $field   The current field to process.
@param bool|string      $newFile The new file path.
@param array            $options The configuration options defined by the user.

@return bool
Delete the old upload file before to save the new file .