how rename image before upload in Yii framework - yii

I have follow yii site to work with upload image, code here:
class ItemController extends CController
{
public function actionCreate()
{
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs('path/to/localFile');
// redirect to success page
}
}
$this->render('create', array('model'=>$model));
}
}
however how can I rename file by currentdate+filename.png and upload to path,also I need code for update and delete.
thankyou very much
I have resolve this problem:
public function currentDate(){
$date = date('m-d-Y-h-i-s', time());
return $date;
}
public function actionCreate(){
$model = new News();
if(isset($_POST['News']))
{
$model->attributes=$_POST['News'];
$uploadedFile = CUploadedFile::getInstance($model, 'images');
$fileName = "{$this->currentDate()}-{$uploadedFile}";
$model->images = $fileName;
if($model->save()){
$uploadedFile->saveAs("upload/".$fileName);
$this->redirect(array('news/index'));
}else{
$model = new News();
$this->render('create',
array('model' =>$model,
'result'=>'insert new fail !',
));
}
}else{
$this->render('create',
array(
'model'=>$model,
));
}
}

public function actionCreate()
{
$model=new News;
if(isset($_POST['News']))
{
$model->attributes=$_POST['News'];
$name = $_FILES['News']['name']['images'];
$filename = pathinfo($name, PATHINFO_FILENAME);
$ext = pathinfo($name, PATHINFO_EXTENSION);
$newName = date("m-d-Y-h-i-s", time())."-".$filename.'.'.$ext;
$model->images = CUploadedFile::getInstance($model,'images');
if($model->save())
$fullImgSource = Yii::getPathOfAlias('webroot').'/upload/'.$newName;
$model->images->saveAs($fullImgSource);
$model->images = $newName;
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array('model'=>$model,));
}

To rename the file after upload and update in DB, try this code.
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
if($model->save())
{
$imageName = #$_FILES["MenuItems"]["name"]["image"];
$uniqueName = (imageName . $model->id) . '.' . (end(explode('.', $imageName)));
$model->image=CUploadedFile::getInstance($model,'image');
$model->image->saveAs('path/to/localFile/'.$uniqueName);
$model->image = $uniqueName;
$model->save();
// redirect to success page
}
}
$this->render('create', array('model'=>$model));

you can use this method that I had created later to upload file and change its name before upload it :
public static function createAttach($model, $imageAttrName) {
$model->$imageAttrName = CUploadedFile::getInstance($model, $imageAttrName);
$fecha = date('YmdHms');
if ($model->$imageAttrName) {
$attach_src = Yii::app()->basePath . '/../upload/' . $fecha.'.'.$model->$imageAttrName->getExtensionName(); //. '_' . $model->$imageAttrName;
$model->$imageAttrName->saveAs($attach_src);
$model->$imageAttrName = $fecha.'.'.$model->$imageAttrName->getExtensionName();// . '_' . $model->$imageAttrName;
}
}

Related

signin page redirecting again to signin page in codeigniter

Controller
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Signin extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('cias');
$this->load->model('home_model');
$this->load->model('signin_model');
}
public function index(){
$this->is_signed_in();
}
function is_signed_in()
{
$is_signed_in = $this->session->userdata('is_signed_in');
if(!isset($is_signed_in) || $is_signed_in != TRUE)
{
// header
$data['logo'] = $this->home_model->get_logo_by_id();
// footer
$data['contact']=$this->home_model->get_contact();
$this->load->view('front/signin');
}
else
{
redirect('front/dashboard');
}
}
public function signinme()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|max_length[128]|trim');
$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$email = strtolower($this->security->xss_clean($this->input->post('email')));
$password = $this->input->post('password');
$result = $this->signin_model->sign_in_me($email, $password);
if(!empty($result))
{
$session_array = array('user_id'=>$result->user_id,
'name'=>$result->name,
'email'=>$result->email,
'phone'=>$result->phone,
'is_signed_in' => TRUE );
$this->session->set_userdata('logged_in', $session_array);
redirect('./dashboard');
}
else
{
$this->session->set_flashdata('error', 'Email Address or password mismatch');
$this->index();
}
}
}
}
Model
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Signin_model extends CI_Model
{
// This function used to check the login credentials of the user
function sign_in_me($email, $password)
{
$this->db->select('*');
$this->db->from('user_login');
$this->db->where('email', $email);
$this->db->where('isdeleted', 0);
$query = $this->db->get();
$user = $query->row();
if(!empty($user)){
if(verifyHashedPassword($password, $user->password)){
return $user;
} else {
return array();
}
} else {
return array();
}
}
function get_user_info_id($user_id){
$this->db->select('*');
$this->db->from('user_login');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query->row();
}
}
Want to redirect
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/libraries/FrontController.php';
class Dashboard extends FrontController {
public function __construct(){
parent::__construct();
$this->load->helper('cias');
$this->load->model('home_model');
$this->load->model('signin_model');
$this->is_signed_in();
}
public function index(){
$this->load->view("front/dashboard", $data);
}
function signout() {
$this->session->sess_destroy ();
redirect ( 'signin' );
}
}

Undefined variable $request

I'm trying to write a clean code in laravel By using trait For uploading img And I Have A Problem In The code
This Is The Controller
I Used this Also So It Can Work in The Controller
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Traits\Uploadimg;
use Illuminate\Http\Request;
use App\Http\Requests\Userstore;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
This Is The Controller
public function store(Userstore $request)
{
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
$user->uimg = $request->uimg;
$this ->uimg($request -> uimg , 'uploads/users');
// $file_extension = $request -> uimg -> getclientoriginalExtension();
// $file_name = time ().'.'.$file_extension;
// $path = 'uploads/users';
// $request -> uimg -> move($path,$file_name);
$user->save();
return redirect()->back()->with(['success' => 'User has been added']);
}
This is the Trait File
<?php
namespace App\Traits;
Trait Uploading
{
function uimg(){
if($request->hasFile('uimg')){
$file = $request->file('uimg');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('uploads/users/' , $filename);
$user->uimg = $filename;
}
else{
return $request;
$user->uimg = '';
}
// $file_extension = $request -> uimg -> getclientoriginalExtension();
// $filename = time ().'.'.$file_extension;
// $path = 'uploads/users';
// $request -> uimg -> move($path,$filename);
// return $filename;
}
}
I Want to Add the img to The Database Who Can i Do This

How to add a custom input field in Admin Product page

How to add custom input field in Product image form using hooks.I am trying to add a new check box in product image form but i do not know how to create it by using modules as well as i am not able to override core product page template.I am createing directory structure inside themes/classic/module/module_name/.....
If some can write the main module php file of Prestashop 1.7 I am very thankful to you.
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class youmodule extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'youmodule';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('youmodule');
$this->description = $this->l('youmodule');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('displayAdminProductsExtra');
}
public function uninstall()
{
return parent::uninstall();
}
public function hookDisplayAdminProductsExtra($params)
{
$id_product = Tools::getValue('id_product');
//YOURCODE
$this->smarty->assign(array(
'yourvariable' => $yourvariabl
));
return $this->display(__FILE__, '/views/templates/admin/product.tpl');
}
}

Yii auth with aply dynamic bizrules

I am using Yii auth extension for user's authentication wise can get access.
In initial level of auth module only describe give operations,assignment,task according to user role.
I want give task according to bizrule at assign by user can access(update,listing,delete) own data.
I want to change one file for apply bizrule
AuthFilter.php
class AuthFilter extends CFilter
{
public $params = array();
public $enableBizRule = true;
public $enableBizRuleData = true;
protected function preFilter($filterChain)
{
$itemName = '';
$controller = $filterChain->controller;
$user = Yii::app()->getUser();
if (($module = $controller->getModule()) !== null) {
$itemName .= $module->getId() . '.';
if ($user->checkAccess($itemName . '*')) {
return true;
}
}
$itemName .= $controller->getId();
//print_r($itemName);
if ($user->checkAccess($itemName . '.*')) {
return true;
}
$itemName .= '.' . $controller->action->getId();
if ($user->checkAccess($itemName, $this->params)) {
return true;
}
if ($user->isGuest) {
$user->loginRequired();
}
throw new CHttpException(401, Yii::t('yii', 'You are not authorized to perform this action.'));
}
}
http://www.yiiframework.com/extension/auth/
http://www.cniska.net/yii-auth/en_us/auth/assignment/index

Yii not able to save Image files CFileUploaded

I am not able to save the image uploaded from simple Form with this code
public function actionImage()
{
print_r($_FILES);
$dir = Yii::getPathOfAlias('application.uploads');
if(isset($_POST['img']))
{
$model = new FileUpload();
$model->attributes = $_POST['img'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->validate())
{
$model->image->saveAs($dir.'/'.$model->image->getName());
// redirect to success page
}
}
}
To Answer my own question instead of using above code I used this:
public function actionImage()
{
$dir = Yii::getPathOfAlias('application.uploads');
if (isset($_FILES['img']))
{
$image = CUploadedFile::getInstanceByName('img');
$image->saveAs($dir.'/'.$image->getName());
}
}
To Answer my own question instead of using above code I used this:
public function actionImage() {
$dir = Yii::getPathOfAlias('application.uploads');
if (isset($_FILES['img']))
{
$image = CUploadedFile::getInstanceByName('img');
$image->saveAs($dir.'/'.$image->getName());
} }