Upload - Verot

Hi,

This topic is for verot.net upload class with image handle.

Documentation:




// import the class

Yii::import('application.extensions.upload.Upload');


// receive file from post

$Upload = new Upload( (isset($_FILES['Filedata']) ? $_FILES['Filedata'] : null) );

$Upload->jpeg_quality  = 100;

$Upload->no_script     = false;

$Upload->image_resize  = true;

$Upload->image_x       = 700;

$Upload->image_y       = 500;

$Upload->image_ratio   = true;


// some vars

$newName  = date('YmdHis');

$destPath = Yii::app()->getBasePath().'/../images/gallery/';

$destName = '';


// verify if was uploaded

if ($Upload->uploaded) {

    $Upload->file_new_name_body = $newName;			

    $Upload->process($destPath);

	

	// if was processed

	if ($Upload->processed) {

            $destName = $Upload->file_dst_name;    

        

            // write image filename on table

            $photo = new Photo();

            $photo->gallery = $_POST['gallery_id'];

            $photo->image_normal = $destName;

            $photo->image_thumb  = 'thumb_' . $destName;

            $photo->save();

        

            // create the thumb  

            unset($Upload);			  

        

            $Upload = new Upload($destPath.$destName);

            $Upload->file_new_name_body   = 'thumb_' . $newName;

            $Upload->no_script            = false;

            $Upload->image_resize         = true;

            $Upload->image_x              = 120;

            $Upload->image_y              = 80;

            $Upload->image_ratio          = true;

            $Upload->process($destPath);

		  

	} else {

            echo($Upload->error)

	}

} else {

    echo('Select a file to send');

}



Some samples:

http://www.verot.net/php_class_upload_samples.htm

I make the support for EXTENSION DOWNLOAD command.

You can get with console command:

extension install upload

You can see how to use this command, here:

http://www.yiiframework.com/forum/index.php?/topic/7129-component-extension/

hello,

this is great… i’d like to be able to use it too! :)

is it ok to use it with an activeFileField like this?




	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'immagine'); ?>

		<?php echo CHtml::activeFileField($model,'immagine',array('size'=>60,'maxlength'=>320)); ?>

		<?php echo CHtml::error($model,'immagine'); ?>

	</div>



my Controller can’t find the file; how should i use this line ?


$Upload = new Upload( (isset($_FILES['immagine']) ? $_FILES['immagine'] : null) );

$_FILES[‘immagine’] is always null! :(

thanks!

where is the class Photo?

Sorry, I think it is a model. :lol:

Hi.

This is an example, that show how to store the filename on database.

And joeysantiago:

You have to create a form with enctype=multpart, like this:


<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

...

<input type="file" name="immagine" />

...

<?php echo CHtml::endForm(); ?>

thanks for this great ext class, it very help me

i want to ask, the upload is file success but i can’t rezise the image????

Can I somehow GET properties of an uploaded file? Just reading properties $upload->image_x gives me some strange results.

I have found solution by myself. All the essentials are here: http://www.verot.net/res/sources/class.upload.html

Hello. I’m testing this out on my WAMP(Windows Apache MySQL PHP) installation and I have GD2 enabled but it refuses to do anything but write the image in its original form. :o

My code is not working I don’t know why…

I have this funcition in controller:


public function actionUploadImage($id)

	{	

		

		// import the class

		Yii::import('application.extensions.upload.Upload');

	

		echo "<pre>";

		print_r($_FILES['Filedata']);		

		echo "</pre>";


		// receive file from post

		$Upload = new Upload(isset($_FILES['Filedata']));

		

		// some vars

		$newName  = date('YmdHis');		

		$destPath = Yii::app()->request->baseUrl;			

		$destName = '';

		

		// verify if was uploaded

		if ($Upload->uploaded) {			

		    $Upload->file_new_name_body = $newName;                     

		    $Upload->process($destPath);

		        

		        // if was processed

		        if ($Upload->processed) {

		            $destName = $Upload->file_dst_name;    

		        

		            // write image filename on table

		            /*$photo = new Photo();

		            $photo->gallery = $_POST['gallery_id'];

		            $photo->image_normal = $destName;

		            $photo->image_thumb  = 'thumb_' . $destName;

		            $photo->save();*/

		        

		            // create the thumb  

		            unset($Upload);                       

		        

		            $Upload = new Upload($destPath.$destName);

		            $Upload->file_new_name_body   = 'thumb_' . $newName;

		            $Upload->no_script            = false;

		            $Upload->image_resize         = true;

		            $Upload->image_x              = 120;

		            $Upload->image_y              = 80;

		            $Upload->image_ratio          = true;

		            $Upload->process($destPath);

		                  

		        } else {

		            echo($Upload->error);

		        }

		} else {


		 	   

		 	   $this->render('uploadimage',array(

				'model'=>$this->loadModel($id),				

			));

		}

	}

And an input in the view file.

print_r($_FILES[‘Filedata’]) works fine, it returns:


Array

(

    [name] => Nenúfares.jpg

    [type] => image/jpeg

    [tmp_name] => C:\WINDOWS\Temp\php53F0.tmp

    [error] => 0

    [size] => 83794

)

But $Upload->uploaded is not working…Help!

Ok I forgot this:


$Upload = new Upload(isset($_FILES['Filedata']));

to


$Upload = new Upload($_FILES['Filedata']);

Hi, Guys I like this awesome class for image/file manipulation.

I have an auto listing webapp. I have everything working, but not images. So I have spent my time trying to get this class working in yii application following prchakal post. I have two models one for handling listing information and one for handling image information.

In may Listing model I have the relationship




<?php

class Listing extends CActiveRecord

{

/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

                         //Other relationship here …………………………….

                          'images' => array(self::HAS_MANY,'AutoListingImages','listing_id'),

		);

//Other methods here


//The method for image upload

            public function saveImage()

           {

     //import the class for image manipulation from the extension folder

     Yii::import('application.extensions.upload.Upload');


     //Receive file/image data from the post request

     $Upload = new Upload((isset($_FILES['images']) ? $_FILES['images'] : null) );

     $Upload->jpeg_quality = 100;

     $Upload->no_script    = false;

     $Upload->image_resize = true;

     $Upload->image_x      = 300;

     $Upload->image_y      = 250;

     $Upload->image_ratio  = true;


     //some vars

     $rand = rand(1000,9000);

     $newName = $rand;

     $image_path = Yii::app()->getBasePath().'/upload_images/listing_uploads/';

     $image_thumb_path = Yii::app()->getBasePath().'/upload_images/listing_uploads/thumbnails/';

     $destName = '';


     //Verify if was uloaded

     if($Upload->uploaded) {

         $Upload->file_new_name_body = $newName;

         $Upload->process($image_path);


         //if was processed

         if($Upload->processed) {

             $destName = $Upload->file_dst_name;

             $thumbDestName = 'thumb_'.$destName;

             //write image file on the database

             $image = new AutoListingImages();

             $image->image_path = $image_path/$destName;

             $image->image_thumb_path = $image_thumb_path/$thumbDestName;

             $image->listing_id = $_POST[listing_id];

             //$image->create_time = new CDbExpression('NOW()');

             //$image->author_id = $author_id;

             $image->save();


             //Create the thumb

             unset ($Upload);

             $xUpload = new Upload($image_thumb_path.$destName);

             $Upload->file_new_name_body = 'thumb_' . $newName;

             $Upload->no_script    = false;

             $Upload->image_resize = true;

             $Upload->image_x      = 85;

             $Upload->image_y      = 50;

             $Upload->image_ratio  = true;

             $Upload->process($image_thumb_path);

         }  else {

             echo ($Upload->error);

         }

     }  else {

         echo "Select an image to upload";

     }

   }




In my ListingImages model I have




<?php

class ListingImages extends CActiveRecord

{

/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('image_path, image_thumb_path', 'required'),

			array('main_image, listing_id', 'numerical', 'integerOnly'=>true),

			array('image_path, image_thumb_path', 'length', 'max'=>255),


			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('listing_id', 'safe', 'on'=>'search'),

		);

	}

/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

                         //Other relationship here …………………………….

                          // NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		'listing' => array(self::BELONGS_TO, 'Listing', 'listing_id'),

		);		);

	}




Here is my controller which is the controller for my model Listing




<?php

class AutoListingsController extends Controller

{

/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

            		$model = new Listing;


		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation($model);


		if(isset($_POST['Listing']))

		{

			$model->attributes=$_POST['Listing'];

                        //This is how you capture those uploaded images.

                        //$model->images = CUploadedFile::getInstance($model, 'images');

			if($model->save())

                        {

                        if($model->images !== null)

                        $model->saveImage();


			$this->redirect(array('view','id'=>$model->id));

                        }

		}

		$this->render('create',array(

	        'model'=>$model,

		));

	}

}




And here is my partial _form.




<div class="form">


<?php $form=$this->beginWidget('CActiveForm',array(

	'id'=>'listing-form',

        'enableClientValidation' => true,

	'enableAjaxValidation'=>true,

        'htmlOptions'=>array('enctype'=>'multipart/form-data'),

)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>


<div class="row">

    <?php echo $form->labelEx($model,'ad_title'); ?>

    <?php echo $form->textField($model,'ad_title',array('size'=>30,'maxlength'=>60)); ?>Example. New Toyota corolla in Kayole

    <?php echo $form->error($model,'ad_title'); ?>

</div>

<div class="row">

    <?php echo $form->labelEx($model,'images'); ?>

    <?php //echo CHtml::activeFileField($model, "[0]images");?>

    <br/>

    <?php //echo CHtml::activeFileField($model, "[1]images");?>

    <br/>

    <?php echo CHtml::activeFileField($model, "images");?>

</div>

<div class="row buttons">

<?php echo CHtml::submitButton($model->isNewRecord ? 'Add Listing' : 'Save'); ?>

</div>

<?php $this->endWidget(); ?>


</div><!-- form -->




With this arrangement of models, controllers and the form My images are not uploading and no errors showing up. I don’t know where the issues is. Also how can I support multiple image upload. The only information I need for image table is image_path, image_thumb_path, author_id, listing_id and create_time. Please help.

Hi,

You set see in apache error log.

You can verify if your target path has permission (777) to write.

Single upload or multiple upload doesn’t depends of your server code, you only need use a client jquery plugin (for example) to make the interface for multiple upload support. Your server code is the same for both.

I got it working I had to change




<div class="row">

    <?php echo $form->labelEx($model,'images'); ?>

    <?php //echo CHtml::activeFileField($model, "[0]images");?>

    <br/>

    <?php //echo CHtml::activeFileField($model, "[1]images");?>

    <br/>

    <?php echo CHtml::activeFileField($model, "images");?>

</div>

<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

...

<input type="file" name="images" />

...

<?php echo CHtml::endForm(); ?>

But just used CMultiFileUpload for multiple file upload

[code]

<?php $this->widget('CMultiFileUpload', array(

                //'model'=>$model,

                //'attribute' => 'images',

                'name' => 'images',//if not within a model

                'accept' => 'jpeg|jpg|gif|png', // useful for verifying files

                'duplicate' => 'Duplicate file!', // useful, i think

                'denied' => 'Invalid file type', // useful, i think

                'max' => '3',

                'remove' => 'delete',

                'selected' => 'You selected. $file',

            )); ?>



[/code]

To

Are there any plans to update this extension to use the latest version of the Verot library (0.31)? That version has added the ability to automatically resize watermarks to fit the resulting picture.

Just a doubt that i got. Do the extension accept more than 1 file in the $_FILES?

I want to send a lot of images, at the same time. Is this possible?

It does I use it to upload multiple images in my app. I have a product table and each product can have multiple images.

Hi, I also had the same problem: “I can send it, but I can’t resize the image”.

I solved this way:




...

$image= '';

if ($model->photo=CUploadedFile::getInstance($model,'photo')){

  $image['name'] = $model->photo->name;

  $image['tmp_name'] = $model->photo->tempName;

  $image['size'] = $model->photo->size;

  $image['type'] = $model->photo->type;

  $image['error'] = $model->photo->error;

}




Yii::import('application.extensions.upload.Upload');


$Upload = new Upload($image);

...



In Controller, for example:




public function actionUpdate()

	{

		$model=$this->loadModel();


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Event']))

		{

			$model->attributes=$_POST['Event'];

			

			$image= '';

			if ($model->photo=CUploadedFile::getInstance($model,'photo')){

				//print_r($model->photo).' <br> ';

				//echo($model->photo->tempName).' <br> ';

				$image['name'] = $model->photo->name;

				$image['tmp_name'] = $model->photo->tempName;

				$image['size'] = $model->photo->size;

				$image['type'] = $model->photo->type;

				$image['error'] = $model->photo->error;

			}

			

			if($model->save()){

				// import the class

				Yii::import('application.extensions.upload.Upload');

		

				// receive file from post

				$Upload = new Upload($image, 'pt_BR');

				$Upload->file_overwrite = true; 

				$Upload->image_convert  = 'jpg'; 

				$Upload->jpeg_quality   = 100;

				$Upload->no_script      = false;

				$Upload->image_resize   = true;

				$Upload->image_x        = 300;

				$Upload->image_y        = 300;

				$Upload->image_ratio_crop  = true; 

				//$Upload->image_ratio    = true; 

				//$Upload->image_ratio_fill = true; 

				//$Upload->image_background_color = '#000000';

		

				$Upload->dir_chmod      = 0777;

		

				// some vars

				$newName  = 'f'.$model->id;

				$destPath = Yii::app()->getBasePath().'/../images/events/';

		

				// verify if was uploaded

				if ($Upload->uploaded) {

				    $Upload->file_new_name_body = $newName;                     

				    $Upload->process($destPath);

				

				    // if was processed

				    if ($Upload->processed) {

					$result = 'ok';

					//$result = $Upload->log;

					unset($Upload);

				    } else {

					$result = $Upload->error;

				    }

				} 

				else {

				    $result = 'Select a image to send.';

				}


				if ($result == 'ok'){

				    $this->redirect(array('view','id'=>$model->id));

				} else {

				    echo 'Image: '.$result.'<br>';	

				}

			}

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}



Hi! Hoy can I update to the latest version of class.upload http://www.verot.net/php_class_upload_download.htm?

I have to replace upload.php and it’s done?