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.