Images fail to upload when I create a new item but on updating the newly created item the file is successfully uploaded. Any reasons?
Trending News Model
class TrendingNews extends \yii\db\ActiveRecord
{
const EVENT_ADD_TRENDING_NEWS = 'add-trending-news';
public $featuredFile;
public function init() {
$this->on(self::EVENT_ADD_TRENDING_NEWS, [$this, 'saveToLog']);
}
/**
* #inheritdoc
*/
public static function tableName()
{
return 'trending_news';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['headline_text','news_info', 'news_url', 'author', 'published', 'date_added', 'date_modified'], 'required'],
[['headline_text', 'image_url', 'news_info', 'news_url'], 'string'],
[['date_added', 'date_modified'], 'safe'],
[['author'], 'string', 'max' => 20],
[['published'], 'string', 'max' => 2],
[['featuredFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'headline_text' => 'Headline',
'image_url' => 'Image Url',
'news_info' => 'Source',
'news_url' => 'News Url',
'author' => 'Author',
'published' => 'Published',
'date_added' => 'Date Added',
'date_modified' => 'Date Modified',
'featuredFile' => 'Featured Image',
];
}
public function uploadBanner()
{
if ($this->validate()) {
$ymd = date("Ymd");
$save_path = \Yii::getAlias('#backend') . '/web/uploads/' . $ymd . '/';
if (!file_exists($save_path)) {
mkdir($save_path, 0777, true);
}
$fileName = "trending_".Yii::$app->security->generateRandomString(20);
$this->featuredFile->saveAs($save_path . $fileName .'.' . $this->featuredFile->extension);
$this->image_url = $ymd . '/'. $fileName . '.' . $this->featuredFile->extension;
return true;
} else {
return false;
}
}
public function upload()
{
if ($this->validate()) {
$ymd = date("Ymd");
$save_path = \Yii::getAlias('#backend') . '/web/uploads/' . $ymd . '/';
if (!file_exists($save_path)) {
mkdir($save_path, 0777, true);
}
$fileName = Yii::$app->security->generateRandomString(20);
$this->featuredFile->saveAs($save_path . $fileName .'.' . $this->featuredFile->extension);
$this->image_url = $ymd . '/'. $fileName . '.' . $this->featuredFile->extension;
return true;
} else {
return false;
}
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if ($this->isNewRecord) {
$this->date_added = date("YmdHis");
$this->author = Yii::$app->user->identity->id;
}
else {
$this->date_modified = date("YmdHis");
}
return true;
}
else{
return false;
}
}
public function saveToLog($event)
{
//assigning attributes
// echo 'mail sent to admin using the event';
$app_log_model = new AppLog();
$app_log_model->log_time = date("YmdHis");
$app_log_model->log_activity = 'Added a trending news';
$app_log_model->user_id = Yii::$app->user->identity->id;
$app_log_model->device = "1";
if ($app_log_model->save()) {
return true;
} else {
return $app_log_model->getErrors() ;
}
}
}
usage
public function actionCreate()
{
$model = new TrendingNews();
if ($model->load(Yii::$app->request->post())) {
$model->featuredFile = UploadedFile::getInstance($model, 'featuredFile');
if(isset($model->featuredFile)){
$model->upload();
$model->save(false);
} else {
// file is uploaded successfully
$model->save();
//$model->trigger(BlogArticle::EVENT_EDIT_ARTICLE);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->featuredFile = UploadedFile::getInstance($model, 'featuredFile');
if(isset($model->featuredFile)){
$model->upload();
$model->save(false);
} else {
$model->save();
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
the app log shows this
$_FILES = [
'TrendingNews' => [
'name' => [
'featuredFile' => 'another_evento.jpg'
]
'type' => [
'featuredFile' => ''
]
'tmp_name' => [
'featuredFile' => ''
]
'error' => [
'featuredFile' => 3
]
'size' => [
'featuredFile' => 0
]
]
]
featuredFile is not a database field
Is there something that is being done wrong?
I am using xampp,tried on a live server and the issue is same
You are doing it wrong if you have to require a file input on insert and not on the update you should add a scenario for it so that it asks you to provide file when adding a new record only.
Secondly you are using the same ActiveRecord model and added a custom attribute in it to be used as a file input and in the upload() function you are calling validate() and returning false if not validated but on the same time you are not checking for true or false in you controller action and on the very next line you are calling $model->save(false) with false so technically your script would never intimate the user if there is any validation error.
The file should be uploaded after the record is saved not before saving the record so that there are no extra files uploaded in case of error, although you want the file to be required on insert so technically if file is not uploaded the record should not be saved, and for this reason we have transactions for database insertions you should use a transaction block to save the rcord and the file along with it
You should only call the validate() method for file uploading if you have a separate FormModel for file uploading otherwise you should load the instance from the UploadedFile to the model field and call the $model->save() which will automatically validate the model before saving you should just check for the empty filename before uploading so that when you are updating any record and not submitting a file the previous file should be kept as is.
You need to update your validation rules to the following first
/**
* #inheritdoc
*/
public function rules()
{
return [
[['headline_text','news_info', 'news_url', 'author', 'published', 'date_added', 'date_modified'], 'required'],
[['headline_text', 'image_url', 'news_info', 'news_url'], 'string'],
[['date_added', 'date_modified'], 'safe'],
[['author'], 'string', 'max' => 20],
[['published'], 'string', 'max' => 2],
[ [ 'featuredFile' ] , 'required' , 'on' => 'insert' ] ,
[ [ 'featuredFile' ] , 'file' , 'extensions' => 'png, jpg' , 'maxSize' => 200000 , 'tooBig' => 'Limit is 500KB' ] ,
];
}
Change the upload() function to the following
public function upload( $ymd , $fileName ) {
if ( $this->featuredFile !== null && $this->featuredFile->name !== '' ) {
$save_path = \Yii::getAlias ( '#backend' ) . '/web/uploads/' . $ymd . '/';
if ( !file_exists ( $save_path ) ) {
mkdir ( $save_path , 0777 , true );
}
if ( !$this->featuredFile->saveAs ( $save_path . $fileName ) ) {
$this->addError ( 'featuredFile' , 'File could not be uploaded' );
throw new \Exception ( 'File upload error' );
}
}
}
Then add another method inside your model to remove old file in case of new file uploaded on update
public function unlinkOldFile( $filename ) {
if ( $filename !== '' ) {
$save_path = \Yii::getAlias ( '#backend' ) . '/web/uploads/' . $filename;
unlink ( $save_path );
}
}
after that change your create and update actions to the following so that they use transation blocks for database operations
public function actionCreate() {
$model = new TrendingNews(['scenario'=>'insert']);
if ( $model->load ( Yii::$app->request->post () ) ) {
$model->featuredFile = UploadedFile::getInstance ( $model , 'featuredFile' );
if ( $model->featuredFile !== null ) {
$ymd = date ( "Ymd" );
$fileName = Yii::$app->security->generateRandomString ( 20 ) . '.' . $model->featuredFile->extension;
$model->image_url = $ymd . '/' . $fileName;
}
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$model->save () ) {
throw new \Exception ( 'Error Occoured' );
}
$model->upload ( $ymd , $fileName );
$transaction->commit ();
return $this->redirect ( [ 'view' , 'id' => $model->id ] );
} catch ( \Exception $ex ) {
$transaction->rollBack ();
}
}
return $this->render ( 'create' , [
'model' => $model ,
] );
}
public function actionUpdate( $id ) {
$model = $this->findModel ( $id );
if ( $model->load ( Yii::$app->request->post () ) ) {
$model->featuredFile = UploadedFile::getInstance ( $model , 'featuredFile' );
//$oldFile = '';
$oldFile = $model->image_url;
if ( $model->featuredFile !== null ) {
$ymd = date ( "Ymd" );
$fileName = Yii::$app->security->generateRandomString ( 20 ) . '.' . $model->featuredFile->extension;
$model->image_url = $ymd . '/' . $fileName;
}
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$model->save () ) {
throw new \Exception ( 'Model error' );
}
$model->upload ( $ymd , $fileName );
$model->unlinkOldFile ( $oldFile );
$transaction->commit ();
return $this->redirect ( [ 'view' , 'id' => $model->id ] );
} catch ( Exception $ex ) {
$transaction->rollBack ();
}
}
return $this->render ( 'update' , [
'model' => $model ,
] );
}
Related
I am using yii2 basic and wanted to make a gallery, so I made use of multi file upload. All working fine, till I added a text input ('year').
GalleryController.php
public function actionCreate()
{
$model = new MultipleUploadForm();
$year;
if (Yii::$app->request->isPost) {
$model->files = UploadedFile::getInstances($model, 'files');
if ($model->gaUpload()) {
return $this->redirect(['index']);
}
}
return $this->render('create', ['model' => $model]);
}
MultipleUploadForm :
class MultipleUploadForm extends Model
{
/**
* #var UploadedFile[] files uploaded
*/
public $files;
public $year;
public function rules()
{
return [
[['files'], 'file', 'skipOnEmpty' => false, 'maxFiles' => 0],
[['year'], 'string'],
];
}
public function gaUpload()
{
if ($this->validate()) {
foreach ($this->files as $file) {
$model2 = new Gallery();
$model2->img = $file->baseName . '_' . rand(100,999) . '.' . $file->extension;
$model2->save_dir = 'uploads/gallery/';
$model2->year = $this->year;
$file->saveAs($model2->save_dir . $model2->img);
$model2->save();
}
return true;
} else {
return false;
}
}
}
Gallery:
class Gallery extends \yii\db\ActiveRecord
{
/**
* {#inheritdoc}
*/
public static function tableName()
{
return 'cgallery';
}
/**
* {#inheritdoc}
*/
public function rules()
{
return [
[['year', 'img', 'save_dir'], 'required'],
[['img', 'save_dir', 'year'], 'string', 'max' => 1024],
];
}
/**
* {#inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'year' => 'Year',
'img' => 'Img',
'save_dir' => 'Save Dir',
];
}
}
_form.php:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], ]); ?>
<?= $form->field($model, 'year')->textInput()?>
<?php
echo '<label class="control-label">Add Pictures</label>';
echo FileInput::widget([
'model' => $model,
'attribute' => 'files[]',
'pluginOptions' => [
'showRemove' => false,
'uploadLabel' => 'Save',
'uploadIcon' => '',
'browseLabel' => '',
'fileExtensions' => 'any',
],
'options' => ['multiple' => true]
]);
?>
<?php ActiveForm::end(); ?>
If I take 'year' out of the process, like : $model2->year = '2010' it work's just fine. I might need to add, that the files get uploaded (into the dir), but it won't get saved to the database.
Has anyone an idea what i did wrong?
EDIT:
Thanks to Wynton Franklin for the help.
The solution was to add the line:
$model->load(\Yii::$app->request->post());
Changed GalleryController.php:
public function actionCreate()
{
$model = new MultipleUploadForm();
$model->load(\Yii::$app->request->post());
if (Yii::$app->request->isPost) {
$model->files = UploadedFile::getInstances($model, 'files');
if ($model->gaUpload()) {
return $this->redirect(['index']);
}
}
return $this->render('create', ['model' => $model]);
}
In your controller year is not begin set. Check that.
if (Yii::$app->request->isPost) {
$model->year = $_POST[][] // or however you do it in yii
$model->files = UploadedFile::getInstances($model, 'files');
if ($model->gaUpload()) {
return $this->redirect(['index']);
}
}
I tried creating a multiple image upload along with a post, but I constantly get an error on count function when I try to count the elements in the array. For the single image, the code works fine if I remove that array and counts. Lumen latest version, PHP version 7.3 but I downgraded it to 7.1.
Code:
<?php
class PostController extends Controller {
public function __construct() {
$this->middleware('auth');
}
public function index() {
return Auth::user()->post;
}
public function show($post_id) {
$post = Post::findOrFail($post_id);
if (Auth::user()->id !== $post->user_id) {
return response()
->json(['status' => 'error', 'message' => 'unauthorized'], 401);
}
return $post;
}
public function store(Request $request) {
$post = new Post;
$post->setConnection('mysql1');
$user = User::where('token', $request->token)
->first();
if ($user) {
$post1 = $post->create(['post_id' => rand() , 'title' => $request->title, 'cooked_time' => $request->cooked_time, 'user_id' => $request->token, 'location' => $user['name'], 'dispose_time' => strtotime('+01 hours', strtotime($request->cooked_time)) , 'food_type' => $request->food_type, 'description' => $request->description, 'serve_quantity' => $request->serve_quantity, 'lat' => $request->lat, 'long' => $request->long, ]);
$post = Post::where('user_id', $request->token)
->orderBy('id', 'DESC',)
->limit('1')
->get();
if ($request->hasFile('image')) {
// $image_array = [];
$image_array = $request->file('image');
$array_len = count($image_array);
for ($i = 0;$i < $array_len;$i++) {
$image_size = $image_array[$i]->getClientSize();
$image_ext = $image_array[$i]->getClientOriginalExtension();
$new_image_name = rand(123456, 99999999) . "." . $image_ext;
if (!function_exists('public_path')) {
/**
* Return the path to public dir
*
* #param null $path
*
* #return string
*/
function public_path($path = null) {
return rtrim(app()->basePath('public/' . $path) , '/');
}
}
$destination_path = public_path('/images');
$image_array[$i]->move($destination_path, $new_image_name);
$image = new PostImage;
$images->image_name = $new_image_name;
$images->post_id = $post1['post_id'];
$images->save();
}
}
return response()
->json(['message' => 'success', 'user' => $user['name'], 'post' => $post1, ], 200);
}
}
public function update(Request $request, $post_id) {
$post = Board::find($post_id);
if (Auth::user()->id !== $post_id->user_id) {
return response()
->json(['status' => 'error', 'message' => 'unauthorized'], 401);
}
$post->update($request->all());
return response()
->json(['status' => 'success', 'post' => $post], 200);
}
public function destroy($id) {
$post = Post::find($post_id);
if (Auth::user()->id !== $post->user_id) {
return response()
->json(['status' => 'error', 'message' => 'unauthorized'], 401);
}
if (Post::destroy($id)) {
return response()->json(['status' => 'success', 'message' => 'Post Deleted Successfully'], 200);
}
return response()
->json(['status' => 'error', 'message' => 'Something Went Wrong']);
}
}
http://php.net/manual/en/migration72.incompatible.php#migration72.incompatible.warn-on-non-countable-types
Try to change
count($image_array)
To
count((is_countable($image_array)?$image_array:[]))
And use this trick for all count() calls
I saw red some answers on similar questions but still can't understand where is my problem. I have a _form.php which is used when data row is created and updated аlso. When i create data row it is OK but when it redirects me to return $this->redirect(['view', 'id' => $model->id]); but the data is not updated. Tried to var_dump($model->getErrors()) (as I saw in the answers of another question) but it returns me and empty array.
These are my files:
Controller action:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$settings = new Settings();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$languages = Lang::find()->all();
foreach ($languages as $language) {
if ($language->default != 1) {
$names = 'names_' . $language->url;
$varNames = Yii::$app->OutData->sanitize($model->$names);
$model->$names = $varNames;
$review = 'review_' . $language->url;
$varReview = Yii::$app->OutData->sanitize($model->$review);
$model->$review = $varReview;
$metaDesc = 'meta_desc_' . $language->url;
$varMetaDesc = Yii::$app->OutData->sanitize($model->$metaDesc);
$model->$metaDesc = $varMetaDesc;
$url = 'url_' . $language->url;
$varUrl = Yii::$app->OutData->sanitize($model->$url);
$model->$url = $varUrl;
$cBirth = 'country_birth_' . $language->url;
$varcBirth = Yii::$app->OutData->sanitize($model->$cBirth);
$model->$cBirth = $varcBirth;
}
else
{
$model->names = Yii::$app->OutData->sanitize($model->names);
$model->review = Yii::$app->OutData->sanitize($model->review);
$model->meta_desc = Yii::$app->OutData->sanitize($model->meta_desc);
$model->url= Yii::$app->OutData->sanitize($model->url);
$model->country_birth = Yii::$app->OutData->sanitize($model->country_birth);
}
}
//записване на изображенията + thumb
if (isset($_POST["Author"]["imageFiles"]) and ! empty($_POST["Author"]["imageFiles"])) {
$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
if (isset($model->imageFiles) and count($model->imageFiles) > 0) {
foreach ($model->imageFiles as $key => $file) {
$parseProdTitle = MakeURL::parseImageName($model->names.'_'.$model->id);
$fileName = $parseProdTitle . '_' . $model->id . '.' . $file->extension;
$fileName = Yii::$app->translate->cyr_to_lat($fileName);
$model->filename = $fileName;
$model->save(false);
$pic = Yii::getAlias('#frontend/web') . '/authors/thumb-270/' . $fileName;
$pic2 = Yii::getAlias('#frontend/web') . '/authors/' . $fileName;
$file->saveAs(Yii::getAlias('#frontend/web') . '/authors/' . $fileName);
$image = file_get_contents(Yii::getAlias('#frontend/web') . '/authors/' . $fileName);
file_put_contents($pic, $image);
$model->resizeImg($pic);
$settings->compress($pic, $pic, 90);
$settings->compress($pic2, $pic2, 90);
}
}
}
if($model->update()){
var_dump(1);die;
}else{
var_dump($model->getErrors());die;// it dumps here but it returns an empty array
}
if($model->validate()){
var_dump(1);die;// it dumps here so validate is ok ( I guess )
}else{
var_dump($model->getErrors());die;
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
And my model:
<?php
namespace backend\models;
use Yii;
use omgdef\multilingual\MultilingualBehavior;
use omgdef\multilingual\MultilingualQuery;
use kartik\helpers\Html;
/**
* This is the model class for table "author".
*
* #property integer $id
* #property integer $active
* #property string $filename
* #property integer $sort
* #property data $birthday
*
* #property AuthorLang[] $authorLangs
*/
class Author extends \yii\db\ActiveRecord
{
public $imageFiles;
private $languages = array();
public function __construct() {
foreach(Yii::$app->params['languages'] as $langArr){
$langParam = new Lang;
$langParam->id = $langArr['id'];
$langParam->url = $langArr['url'];
$langParam->local = $langArr['local'];
$langParam->name = $langArr['name'];
$langParam->default = $langArr['default'];
$langParam->active = $langArr['active'];
$this->languages[] = $langParam;
}
parent::__construct();
}
public static function find()
{
return new MultilingualQuery(get_called_class());
}
public function behaviors()
{
$languagesArray = [];
foreach($this->languages as $language){
if($language->default){
$defLang = $language->url;
}
$languagesArray[$language->local] = $language->name;
}
return [
'ml' => [
'class' => MultilingualBehavior::className(),
'languages' => $languagesArray,
//'languageField' => 'language',
//'localizedPrefix' => '',
//'requireTranslations' => false',
//'dynamicLangClass' => true',
//'langClassName' => PostLang::className(), // or namespace/for/a/class/PostLang
'defaultLanguage' => $defLang,
'langForeignKey' => 'author_id',
'tableName' => "{{%authorLang}}",
'attributes' => [
'names',
'review',
'meta_desc',
'url',
'country_birth',
]
],
];
}
/**
* #inheritdoc
*/
public static function tableName()
{
return 'author';
}
/**
* #inheritdoc
*/
public function rules()
{
$required = ['names', 'review', 'meta_desc', 'url', 'birthday', 'country_birth'];
$this->checkLanguage([$required]);
return [
[['active', 'sort'], 'required'],
[$required, 'required'],
['names', 'string', 'max' => 255],
['country_birth', 'string', 'max' => 255],
['review', 'string'],
['meta_desc', 'string', 'max' => 170],
['url', 'string', 'max' => 60],
[['active', 'sort'], 'integer'],
[['filename'], 'string'],
];
}
protected function checkLanguage($megaArr = [])
{
foreach ($this->languages as $language)
{
if($language->default != 1)
{
foreach ($megaArr as $fields)
{
foreach ($fields as $field)
{
$field .= '_' . $language->url;
}
}
}
}
}
public function changeActiveForm() {
$active = "";
if ($this->active == 1) {
$active = 'checked="checked"';
}
return '<label class="switch switch-custom block mbn taCenter">
<input type="checkbox" value="1" id="check_' . $this->id . '" name="field_types" class="legend-switch" ' . $active . ' onchange="changeStatusActive(' . $this->id . ', \'author\');"></input>
<label data-off="' . Yii::t('app', 'Не') . '" data-on="' . Yii::t('app', 'Да') . '" for="check_' . $this->id . '"></label>
<span></span>
</label>';
}
public function getKartikImagesList() {
$doctorImagesArr = array();
$doctorImages = Author::find()->where('id = :id', [':id' => $this->id])->orderBy(['id' => SORT_ASC])->one();
if(isset($doctorImages->filename) and $doctorImages->filename!="" and $doctorImages->filename!=Null) {
$fileName = Yii::getAlias('#frontend/web') . "/authors/" . $doctorImages->filename;
if (file_exists($fileName)) {
$fileNameUrl = Yii::$app->urlManagerFrontend->baseUrl . "/authors/thumb-270/" . $doctorImages->filename;
$doctorImagesArr[] = Html::img($fileNameUrl, ['class' => 'file-preview-image', 'style' => 'width: 350px;']) .
'<span class="glyphicons glyphicons-bin"></span>';
}
}
return $doctorImagesArr;
}
public function resizeImg($img) {
$sz = getimagesize($img);
$ratio = $sz[0] / $sz[1]; // w/h
$w2 = Yii::$app->params['thumbswidth']; // thumb 1 width
$image = new SimpleImage();
$image->load($img);
$image->resize($w2, round($w2 / $ratio));
$image->save($img);
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'active' => Yii::t('app','app.Active' ),
'filename' => Yii::t('app','app.Filename' ),
'sort' => Yii::t('app','app.Sort' ),
'country_birth' => Yii::t('app','app.Birth Country' ),
'names' => Yii::t('app','app.Names' ),
'meta_desc' => Yii::t('app','app.Meta Desc' ),
'filename' => Yii::t('app','app.Image' ),
'birthday' => Yii::t('app','app.Birthday' ),
'imageFiles' => Yii::t('app','app.Image' ),
'description' => Yii::t('app','app.Review' ),
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getAuthorLangs()
{
return $this->hasMany(AuthorLang::className(), ['author_id' => 'id']);
}
}
I didn't post the _form.php because it is the same for both actions (create and update) and I think that the problem is not in it.If you need it will update my question immediately. Thank you in advance!
For some reason it is possible that the update does not affect any row in the table. In this case the function return 0 .. for check this situation you should chek this this way
http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html#update()-detail
if($model->update() !== false){
var_dump(1);die;
}else{
var_dump($model->getErrors());die;// it dumps here but it returns an empty array
}
then, just for debugging, you could also try using $model->save(false) instead of $model->update() ... this way the $model is saved without regarding the validation rules ..
i have a mistake uploading some images on "multiple upload". If i try to upload the galeries without these images the widget works correctly.
The Image: http://s27.postimg.org/a6qosyctv/Esta_Imagen_Tira_Error.jpg
My form:
$form = ActiveForm::begin([
'id' => 'Item',
'layout' => 'horizontal',
'enableClientValidation' => false,
'errorSummaryCssClass' => 'error-summary alert alert-error',
'options' => ['enctype'=>'multipart/form-data']
]);
$form->field($gallery, 'images[]')->widget(\kartik\file\FileInput::classname(), [
'options' => [
'multiple' => true,
],
'pluginOptions' => [
'uploadUrl' => 'javascript:;',
'showCaption' => false,
'showUpload' => false,
'overwriteInitial' => false,
'allowedFileExtensions' => ['jpg', 'jpeg', 'png'],
'layoutTemplates' => [
'actionUpload' => '',
],
'browseIcon' => '',
'browseLabel' => Yii::t('app', 'Select Files'),
'browseClass' => 'btn btn-block',
'removeLabel' => Yii::t('app', 'Remove All File'),
'removeClass' => 'btn btn-block',
],
]);
My controller:
public function actionCreate()
{
$model = new Hotel();
$model->setCurrentLanguage();
$model->itemType = IElement::ITEM_TYPE_HOTEL;
if ($model->load($_POST)) {
$model->coverPhoto = UploadedFile::getInstance($model, 'coverPhoto');
if ($model->save()) {
if (isset($_POST['Gallery'])) {
$gallery = new Gallery();
$gallery->setAttributes($_POST['Gallery']);
$gallery->idElement = $model->idItem;
$gallery->itemType = IElement::ITEM_TYPE_HOTEL;
$gallery->images = UploadedFile::getInstances($gallery, 'images');
$gallery->save();
}
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
'gallery' => new Gallery(),
]);
}
Gallery Model:
public $images = [];
public function rules()
{
return array_merge(parent::rules(), [
[['images', 'removedImages'], 'safe']
]);
}
public function save($runValidation = true, $attributeNames = null)
{
$transaction = $this->getDb()->beginTransaction();
if(parent::save($runValidation, $attributeNames)){
foreach($this->images as $image){
/* #var UploadedFile $image */
if(!GalleryItem::generateFromImage($image, $this)){
$transaction->rollBack();
return false;
}
}
if(strlen($this->removedImages) > 0){
foreach(explode(',', $this->removedImages) as $itemId){
$albumItem = GalleryItem::findOne($itemId);
if($albumItem instanceof GalleryItem && !$albumItem->delete()){
$transaction->rollBack();
return false;
}
}
}
$transaction->commit();
return true;
}
$transaction->rollBack();
return false;
}
Gallery Item Model:
public static function generateFromImage($imageFile, $gallery)
{
$image = new self;
$image->idGallery = $gallery->primaryKey;
$image->galleryItemOrder = time();
$path = $image->getBasePath();
if(!file_exists($path)) {
mkdir($path, 0777, true);
}
$name = uniqid().'_'.strtr($imageFile->name, [' '=>'_']);
$imageFile->saveAs($path.DIRECTORY_SEPARATOR.$name);
$image->picture = $name;
// var_dump($image->attributes);die();
if(!$image->save()){
#unlink($path.DIRECTORY_SEPARATOR.$name);
return false;
}
return true;
}
I am trying to add an additional image upload field to a gallery extension. The code for the extension controller is below. I've added an extra column to the database called galpic_popup_image with the same values as the existing galpic_image column.
I've managed to get the upload working, so images are saved on the server, however it isn't saving the filename in the database and therefore I can't call it on the frontend.
class ParadoxLabs_Gallery_Adminhtml_GalleryController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->loadLayout();
$this->_setActiveMenu('cms/gallery');
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Gallery'), Mage::helper('adminhtml')->__('Gallery'));
$this->renderLayout();
}
public function editAction()
{
$this->loadLayout();
$this->_setActiveMenu('cms/gallery');
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Gallery'), Mage::helper('adminhtml')->__('Gallery'));
$this->_addContent($this->getLayout()->createBlock('gallery/adminhtml_gallery_edit'));
$this->renderLayout();
}
public function newAction()
{
$this->editAction();
}
public function saveAction()
{
if ( $this->getRequest()->getPost() ) {
// Image uploading code modified from https://magento2.atlassian.net/wiki/spaces/m1wiki/pages/14024884/How+to+create+an+image+or+video+uploader+for+the+Magento+Admin+Panel
if(isset($_FILES['image']['name']) && (file_exists($_FILES['image']['tmp_name'])))
{
try{
$uploader = new Varien_File_Uploader('image');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS . 'p_gallery' . DS ;
$uploader->save($path, $_FILES['image']['name']);
$data['image'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'p_gallery/'. $_FILES['image']['name'];
}catch(Exception $e){}
}
else
{
if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1){
$data['image_main'] = '';
}
else{
unset($data['image']);
}
}
if(isset($_FILES['popup_image']['name']) && (file_exists($_FILES['popup_image']['tmp_name'])))
{
try{
$uploader = new Varien_File_Uploader('popup_image');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS . 'p_gallery' . DS ;
$uploader->save($path, $_FILES['popup_image']['name']);
$popupdata['popup_image'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'p_gallery/'. $_FILES['popup_image']['name'];
}catch(Exception $e){}
}
else
{
if(isset($popupdata['fileinputname']['delete']) && $popupdata['fileinputname']['delete'] == 1){
$popupdata['popup_image_main'] = '';
}
else{
unset($popupdata['popup_image']);
}
}
try {
if( intval($this->getRequest()->getParam('id')) == 0 ) { // New
$model = Mage::getModel('gallery/gallery')
->setGalpicId ( $this->getRequest()->getParam('id') )
->setGalpicStore( $this->getRequest()->getParam('store') )
->setGalpicDate ( date('Y-m-d', time()) )
->setGalpicName ( $this->getRequest()->getParam('name') )
->setGalpicImage( $data['image'] )
->setGalpicPopupImage( $popupdata['popup_image'] )
->save();
}
else { // Edit
$model = Mage::getModel('gallery/gallery')
->setGalpicId ( $this->getRequest()->getParam('id') )
->setGalpicStore( $this->getRequest()->getParam('store') )
->setGalpicName ( $this->getRequest()->getParam('name') )
->setGalpicImage( $data['image'] )
->setGalpicPopupImage( $popupdata['popup_image'] )
->save();
}
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Galpic was successfully saved'));
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
$this->_redirect('*/*/');
}
public function deleteAction()
{
if( $this->getRequest()->getParam('id') > 0 ) {
try {
$model = Mage::getModel('gallery/gallery');
$model->setGalpicId($this->getRequest()->getParam('id'))
->delete();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Galpic was successfully deleted'));
$this->_redirect('*/*/');
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
protected function _isAllowed()
{
return Mage::getSingleton('admin/session')->isAllowed('cms/gallery');
}
}
And here is the form used to capture the data:
class ParadoxLabs_Gallery_Block_Adminhtml_Gallery_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareLayout()
{
parent::_prepareLayout();
if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
}
}
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'enctype' => 'multipart/form-data',
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
));
$fieldset = $form->addFieldset('gallery_form', array(
'legend' => Mage::helper('gallery')->__('Galpic'),
'class' => 'fieldset-wide'
)
);
$fieldset->addField('galpic_name', 'text', array(
'name' => 'name',
'label' => Mage::helper('gallery')->__('Name'),
'class' => 'required-entry',
'required' => true,
));
$fieldset->addField('galpic_image', 'image', array(
'name' => 'image',
'label' => Mage::helper('gallery')->__('Image'),
'class' => 'required-entry',
'required' => true,
));
$fieldset->addField('galpic_popup_image', 'image', array(
'name' => 'popup_image',
'label' => Mage::helper('gallery')->__('Popup Image'),
'class' => 'required-entry',
'required' => true,
));
$fieldset->addField('galpic_store', 'select', array(
'name' => 'store',
'label' => Mage::helper('core')->__('Store View'),
'title' => Mage::helper('core')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
if (Mage::registry('gallery')) {
$form->setValues(Mage::registry('gallery')->getData());
}
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
Ok, I fixed it!
It was suggested to make sure the columns were in the same order as the controller saves the items so I did that. It didn't seem to have an effect (but it may have done).
I changed the second image upload code to the following (basically removed 'popup' from $popupdata as Kiatng suggested) above:
if(isset($_FILES['popup_image']['name']) && (file_exists($_FILES['popup_image']['tmp_name'])))
{
try{
$uploader = new Varien_File_Uploader('popup_image');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS . 'p_gallery' . DS ;
$uploader->save($path, $_FILES['popup_image']['name']);
$data['popup_image'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'p_gallery/'. $_FILES['popup_image']['name'];
}catch(Exception $e){}
}
else
{
if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1){
$data['image_main'] = '';
}
else{
unset($data['popup_image']);
}
}