[Extension] Xupload

Hi guys i really need help…

im having a hard time to set acceptFileTypes on the UI version, using this extension… how do you guys set restrictions for file types?

<?php

$this->widget( ‘xupload.XUpload’, array(

 'url' =&gt; url('/test/upload'),				


 'model' =&gt; &#036;image,


 'htmlOptions' =&gt; array('id'=&gt;'image-form'),


 'attribute' =&gt; 'name',


 'multiple' =&gt; true,


 'options' =&gt; array(				


      'acceptFileTypes'=&gt;'/&#092;.(png)|(jpg)|(gif)|(jpeg)&#036;/i'


),			

));

?>

this doesnt work though, i get Object /\.(png)|(jpg)|(gif)|(jpeg)$/i has no method ‘test’

http://www.yiiframework.com/forum/index.php/topic/19277-extension-xupload/page__view__findpost__p__155944

thanks asgaroth it works…

btw… is there a way we can view the thumbnails in galleries instead of downloading? and the link on the right side is still download?

or something like bootstrap-image-gallery? (if we can also include this would definitely be super wonderful)

Hi Asgaroth,

in this moment, i am working in a develop uploading images and it works perfectly. however, i want to load many images, then leave the page ans the return to the page to see all that are loaded.

it is my controller




class UploadController extends Controller {

	

	public function actionIndex() {

		Yii::import ( "xupload.models.XUploadForm" );

		$model = new XUploadForm ();

		

		$id = $_GET ["idProducto"];

		

		$producto = Producto::model ()->findByPk ( $id );

		

		$exclude_list = array(".", "..");

		

		$path = realpath ( Yii::app ()->getBasePath () . "/../images/productos/" ) . "\\" . $id . "\\";

		$publicPath = Yii::app ()->getBaseUrl () . "/images/productos/" . $id . "/";

		

		$directories = array_diff(scandir($path), $exclude_list);

		$image = 0;

		foreach($directories as $filename) {

			

			$image_info = @getimagesize($path.$filename);

			

			$imagesize = filesize($path . $filename);

			

			$userImages [] = array ("path" => $path . $filename, //the same file or a thumb version that you generated

					"thumb" => $path.'thumbs/'. $filename,

					"filename" => $filename,

					'size' => $imagesize,

					'mime' => $image_info['mime'],

					'name' => "imagen ".$image );

			

			$data[] = array (

				   "name" => "imagen ".$image, 

				   "type" => $image_info['mime'], 

				   "size" => $imagesize, 

				   "url" => $publicPath . $filename, 

				   "thumbnail_url" => $publicPath . "thumbs/$filename", 

				   "delete_url" => $this->createUrl ( "upload", array ("_method" => "delete", "file" => $filename, "idProducto"=>$IdProducto ) ), 

				   "delete_type" => "POST" );

			

			$image++;

			

		}

		

		$model->filename = $path . $filename;

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

		

		Yii::app ()->user->setState ( 'images', $userImages );

		

		$this->render ( 'index', array ('model' => $model, 'Producto' => $producto, 'DataImg' => json_encode($data) ) );

	}

	

	public function actions() {

		return array ('upload' => array ('class' => 'xupload.actions.XUploadAction', 

				'path' => Yii::app ()->getBasePath () . "/../uploads", 

				'publicPath' => Yii::app ()->getBaseUrl () . "/uploads", 

				'subfolderVar' => "parent_id" 

			) 

		);

	}

	// Uncomment the following methods and override them if needed

	/*

	public function filters()

	{

		// return the filter configuration for this controller, e.g.:

		return array(

			'inlineFilterName',

			array(

				'class'=>'path.to.FilterClass',

				'propertyName'=>'propertyValue',

			),

		);

	}

	*/

	public function actionUpload() {

		Yii::import ( "xupload.models.XUploadForm" );

			

		if(isset( $_GET ['idProducto'])){

			$IdProducto = $_GET ['idProducto'];

			//Here we define the paths where the files will be stored temporarily

			$path = realpath ( Yii::app ()->getBasePath () . "/../images/productos/" ) . "\\" . $IdProducto . "\\";

			$publicPath = Yii::app ()->getBaseUrl () . "/images/productos/" . $IdProducto . "/";

		}

		if (isset ( $IdProducto ) && !isset($_GET ["_method"])) {

			

			//This is for IE which doens't handle 'Content-type: application/json' correctly

			header ( 'Vary: Accept' );

			if (isset ( $_SERVER ['HTTP_ACCEPT'] ) && (strpos ( $_SERVER ['HTTP_ACCEPT'], 'application/json' ) !== false)) {

				header ( 'Content-type: application/json' );

			} else {

				header ( 'Content-type: text/plain' );

			}

			

			$model = new XUploadForm ();

			

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

			//We check that the file was successfully uploaded

			

			if ($model->file !== null) {

				//Grab some data

				$model->mime_type = $model->file->getType ();

				$model->size = $model->file->getSize ();

				$model->name = $model->file->getName ();

				//(optional) Generate a random name for our file

				$filename = md5 ( Yii::app ()->user->id . microtime () . $model->name );

				$filename .= "." . $model->file->getExtensionName ();

				if ($model->validate ()) {

					

					if (! is_dir ( $path )) {

						mkdir ( $path, 0777, true );

						chmod ( $path, 0777 );

						//throw new CHttpException(500, "{$this->path} does not exists.");

					}

					

					if (! is_dir ( $path."/thumbs/" )) {

						mkdir ( $path."/thumbs/", 0777, true );

						chmod ( $path, 0777 );

					}

					

					//Move our file to our temporary dir

					$model->file->saveAs ( $path . $filename );

					chmod ( $path . $filename, 0777 );

					//here you can also generate the image versions you need 

					//using something like PHPThumb

					

					Yii::import('application.extensions.image.Image');

					$image = new Image($path.$filename);

					

	                $image->resize(80, 80)->quality(75);

	                $image->save($path."/thumbs/".$filename);

					

					//Now we need to save this path to the user's session

					if (Yii::app ()->user->hasState ( 'images' )) {

						$userImages = Yii::app ()->user->getState ( 'images' );

					} else {

						$userImages = array ();

					}

					$userImages [] = array ("path" => $path . $filename, //the same file or a thumb version that you generated

					"thumb" => $path . $filename, 

					"filename" => $filename, 

					'size' => $model->size, 

					'mime' => $model->mime_type, 

					'name' => $model->name );

					Yii::app ()->user->setState ( 'images', $userImages );

					

					//Now we need to tell our widget that the upload was succesfull

					//We do so, using the json structure defined in

					echo json_encode ( array (array ("name" => $model->name, "type" => $model->mime_type, "size" => $model->size, "url" => $publicPath . $filename, "thumbnail_url" => $publicPath . "thumbs/$filename", "delete_url" => $this->createUrl ( "upload", array ("_method" => "delete", "file" => $filename, "idProducto"=>$IdProducto ) ), "delete_type" => "POST" ) ) );

				} else {

					//If the upload failed for some reason we log some data and let the widget know

					echo json_encode ( array (array ("error" => $model->getErrors ( 'file' ) ) ) );

					Yii::log ( "XUploadAction: " . CVarDumper::dumpAsString ( $model->getErrors () ), CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction" );

				}

			} else {

				throw new CHttpException ( 500, "Could not upload file" );

			}

		} //Here we check if we are deleting and uploaded file

		else if (isset ( $_GET ["_method"] )) {

			if ($_GET ["_method"] == "delete") {

				if ($_GET ["file"] [0] !== '.') {

					$file = $path . $_GET ["file"];

					if (is_file ( $file )) {

						unlink ( $file );

						unlink ($path."/thumbs/".$file);

					}

				}

				echo json_encode ( true );

			}

		} else {

			throw new CHttpException ( 500, "Ingrese los datos del producto" );

		}

	}

}



this is my view




<?php

/* @var $this UploadController */


$this->breadcrumbs=array(

	'Upload',

);

?>


<div class="form">


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

	'id'=>'upload-index',

	'enableAjaxValidation'=>false,

	//This is very important when uploading files

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

)); ?>

<h1><?php echo $this->id . '/' . $this->action->id; ?></h1>


<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$Producto,

	'attributes'=>array(

		'nombre',

		'descripcion',

		array(

			'name'=>'categoria',

			'value' =>CHtml::encode($Producto->categoria->nombre),

		),

		array(

			'name'=>'proveedor',

			'value' =>CHtml::encode($Producto->proveedor->nombre),

		),

		array(

			'name'=>'productoEstado',

			'value'=>CHtml::encode($Producto->productoEstado->nombre),

		)

	),

)); ?>

	

	<div class="row">

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

            <?php 

            $this->widget( 'xupload.XUpload', array(

                'url' => Yii::app( )->createUrl( "/Upload/upload", array("idProducto"=>$Producto->id)),

                //our XUploadForm

                'model' => $model,

                //We set this for the widget to be able to target our own form

                'htmlOptions' => array('id'=>'upload-index',

						'formData'=>$DataImg				

				),

				'options' => array(//Additional javascript options

	               'completed'=>'js:function(){location.reload(true);}',

                ),

                'attribute' => 'file',

                'multiple' => true,

            	'heigth' => 400,

            	'width' => 400, 

			    //Note that we are using a custom view for our widget

                //Thats becase the default widget includes the 'form' 

                //which we don't want here

                )    

            );

            ?>

    </div>


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

</div>



thank you very much.

greetings from Colombia.

I didn’t quite understand your question, if what you want its to preload a list of images in the widget, please search this thread, people has solved this before.

Hi Asgaroth,

First thank you for your update on xupload-workflow upon my request to add files names. However with that extra help, I am still having some issues.

This is what I have on my test server on top of working xupload demo applied (working):

What I want to do is to have a demo site on top of your xupload extension. Any help please? Thank you!

P.S. Since I was not allowed include any link in my post, I attached a zip file containing all the files for XUpload-Workflow Demo.

is there a way we can view the thumbnails in galleries instead of downloading? and the link on the right side is still download?

This is out of the scope of the extension, however, if many people request it, it may be added. but in any case, if you already have a gallery in javascript, or a plugin that does it, it shouldn’t be hard to plug it in. generally for the gallery plugins to work, you just need to add a class to the images, you can do that easily by overriding the XUpload upload template.

Hi I’m just starting to understand the Yii and ran into a problem installing xupload, did everything by the manual but do not call widget is loaded CSS and Js attached image rghost.ru/41600562/image.png

The styling is not part of the extension itself, the demo uses twitter bootstap, its a matter of adding bootstrap to your app. or you can add your own styles.

Yes, but it also does not upload the files, I thought Chthon running after all the made-to guides http://www.yiiframework.com/extension/xupload/ from this srainy and can use return? or something to build upon their own hands?

I’m sorry, I’m not understanding, English is not may main language so you got to be more descriptive.

Hey, I’m having trouble getting the upload files queue to even populate. After trying to solve this for a while, I decided to try to run the demo, and I have the same problem there. I’m not sure what else I would need to do, I am using the latest version of Yii, there are no javascript errors or anything, I tried multiple browsers, the online demo works great. I click “Add File” and no matter what I choose, nothing happens in the GUI. Any ideas? Thanks!

Not sure what to answer, at least the demo should be working. but without information there is nothing I can help you with.

Thanks asgaroth, great help and support…

Next Question:

"set form data on page load"

So ive already uploaded files and these files are automatically viewed using js_encode of the file properties and xupload immediately shows them on template, but how will i have the same view/result when i visit again the page next time (no uploads done yet)?

i tried doing an echo js_encode of the same properties in the controller, but instead it just printed out the data and was not read by the xupload, unlike what happens right after uploading files…

am i missing something? ive been reading, havent found any answers yet…

I am working on an existing project.

Currently XUpload displays "Delete" possibility to uploaded files.

How can I remove it from there? I understand that there is some template, but mayby there is some config option to disable delete?

there is an option to specify your own template, the default template does not have configuration options

is this not possible?

I haven’t done it myself, so I can’t help you. but I’ve heard of other people doing this already. so to your question, yes - its possible.

thank you :)