controllers/AdminController.php
protected function saveImage($model)
    {
        //$subdir = News::getImageSubdir($model->id);
        $subdir = $this->module->model('News')->getImageSubdir($model->id);
        FileHelper::createDirectory($this->uploadsNewsDir . '/' . $subdir);

        //$baseMainImageName = $model->imagefile->baseName; // don't use original file name
        $baseMainImageName = $model->id . '-' . $this->baseMainImageName; // image name should be standard
        $path = $subdir . '/' . $baseMainImageName . '.' . $model->imagefile->extension;
        $newImagePath = $this->uploadsNewsDir . '/' . $path;
        $oldImagePath = $this->uploadsNewsDir . '/' . $model->image;

        if ($newImagePath !== $oldImagePath && is_file($newImagePath)) { // names collision
            //! do not overwrite: it can be file uploaded by image manager
            //$model->addError('imagefile', 'Such image already exists: ' . $newImagePath); return false; //error can't solve problem
            rename($newImagePath, $newImagePath . '-' . uniqid()); // rename exists old file with 'new' name for save
        }

        if (is_file($oldImagePath)) {
            @unlink($oldImagePath); // always delete old main news image - need when change file extension
        }

        try {
            $model->imagefile->saveAs($newImagePath);
        } catch(ErrorException $e) {
            $model->addError('imagefile', $error = $e->getMessage());
            $path = false;
        }
        return $path;
    }
protected function saveImage($model)
    {
        //$subdir = News::getImageSubdir($model->id);
        $subdir = $this->module->model('News')->getImageSubdir($model->id);
        FileHelper::createDirectory($this->uploadsNewsDir . '/' . $subdir);

        //$baseMainImageName = $model->imagefile->baseName; // don't use original file name
        $baseMainImageName = $model->id . '-' . $this->baseMainImageName; // image name should be standard
        $path = $subdir . '/' . $baseMainImageName . '.' . $model->imagefile->extension;
        $newImagePath = $this->uploadsNewsDir . '/' . $path;
        $oldImagePath = $this->uploadsNewsDir . '/' . $model->image;

        if ($newImagePath !== $oldImagePath && is_file($newImagePath)) { // names collision
            //! do not overwrite: it can be file uploaded by image manager
            //$model->addError('imagefile', 'Such image already exists: ' . $newImagePath); return false; //error can't solve problem
            rename($newImagePath, $newImagePath . '-' . uniqid()); // rename exists old file with 'new' name for save
        }

        if (is_file($oldImagePath)) {
            @unlink($oldImagePath); // always delete old main news image - need when change file extension
        }

        try {
            $model->imagefile->saveAs($newImagePath);
        } catch(ErrorException $e) {
            $model->addError('imagefile', $error = $e->getMessage());
            $path = false;
        }
        return $path;
    }
protected function saveImage ( $ model ) { //$subdir = News::getImageSubdir($model->id); $ subdir = $ this -> module -> model ( 'News' ) -> getImageSubdir ( $ model -> id ) ; FileHelper :: createDirectory ( $ this -> uploadsNewsDir . '/' . $ subdir ) ; //$baseMainImageName = $model->imagefile->baseName; // don't use original file name $ baseMainImageName = $ model -> id . '-' . $ this -> baseMainImageName ; // image name should be standard $ path = $ subdir . '/' . $ baseMainImageName . '.' . $ model -> imagefile -> extension ; $ newImagePath = $ this -> uploadsNewsDir . '/' . $ path ; $ oldImagePath = $ this -> uploadsNewsDir . '/' . $ model -> image ; if ( $ newImagePath !== $ oldImagePath && is_file ( $ newImagePath ) ) { // names collision //! do not overwrite: it can be file uploaded by image manager //$model->addError('imagefile', 'Such image already exists: ' . $newImagePath); return false; //error can't solve problem rename ( $ newImagePath , $ newImagePath . '-' . uniqid ( ) ) ; // rename exists old file with 'new' name for save } if ( is_file ( $ oldImagePath ) ) { @ unlink ( $ oldImagePath ) ; // always delete old main news image - need when change file extension } try { $ model -> imagefile -> saveAs ( $ newImagePath ) ; } catch ( ErrorException $ e ) { $ model -> addError ( 'imagefile' , $ error = $ e -> getMessage ( ) ) ; $ path = false ; } return $ path ; }
Save image file. Replace if exists.
@return string relative path to file without main upload dir prefix
Save image file . Replace if exists .