Yii Framework Forum: Need help for upload file function. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Need help for upload file function. Rate Topic: -----

#1 User is offline   KennyLucia 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-March 17

Posted 14 March 2017 - 07:00 PM

hi admin i need to use the upload function for my web site can you help me.
I want to do upload my Picture to my host. i created the model upload with the name upload.php and put in model folder. like this "\protected\models"

    <?php class Upload extends CActiveRecord {
    public $image;
    // ... other attributes

    public function rules()
        return array(
            array('image', 'file', 'types'=>'jpg, gif, png', 'safe' => false),
    } }

then i made the Controller with the name UploadController.php and put in Controller folder. "\controllers\admin".


<?php class UploadController extends Controller
public $layout='//content';

public function actionCreate()
$model=new Upload;
// redirect to success page
$this->render('/admin/upload', array('model'=>$model));

then made the view like this with name upload.php and put it in view folder
<div class="note">
	<div class="note_title">
		<a href="<?php echo Yii::app()->homeUrl; ?>admin">Admin area</a> &rarr; Add Picture
	<div class="note_body">
		<?php if(Yii::app()->user->hasFlash('message')) echo Yii::app()->user->getFlash('message'); ?>
		<?php $form = $this->beginWidget(
        'id' => 'upload-form',
        'enableAjaxValidation' => false,
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
	<div class="form"><?php echo $form->errorSummary($model); ?></div>
		<table class="table_info">	
				<td><?php echo $form->labelEx($model, 'image');?></td>
				<td><?php echo $form->fileField($model, 'image');?></td>
				<td><?php echo $form->error($model, 'image');?></td>
				<td><?php echo CHtml::submitButton('Submit');?></td>
	<?php $this->endWidget(); ?>

then in my main page main.php i call that upload action with this code
					<a href="<?php echo Yii::app()->homeUrl; ?>admin/upload"><p>Items Upload</p>
					Items Upload</a>

but it not working i don't know what i missing ! i alway said "Error 404 Unable to resolve the request "upload". " what that mean ?

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,829
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 15 March 2017 - 12:19 PM

I think you should start without the admin subdir. It might work but I think you may need controller mapping. (Perhaps what you need is an admin module instead?)

You named the controller UploadController, the action actionCreate, that means the route should become upload/create
Don't forget to read The Definitive Guide to Yii (1.1 en) (1.1 sv) (2.0 en) | The class reference (1.1) (2.0) has the details

#3 User is offline   reykel 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 67
  • Joined: 21-June 15
  • Location:Camaguey, Cuba

Posted 07 April 2017 - 07:48 PM

Try this my friend
on the controller

	public function actionCreate()
		$model=new Imagenes;


			move_uploaded_file($_FILES['archive']['tmp_name'], "imagenes/upload/".$_FILES['archive']['name']);

			$zip = new ZipArchive;

			if($zip->open("imagenes/upload/".base64_encode($_FILES['archive']['name']) , ZipArchive::CREATE) === TRUE){
				$zip->addFile("imagenes/upload/".$_FILES['archive']['name'], $_FILES['archive']['name']);


			$model->imagen = $_FILES['archive']['name'];

			if(Imagenes::model()->find(imagen = ?', array($_FILES['archive']['name'])) === null){



your form:

/* @var $this ImagenesController */
/* @var $model Imagenes */
/* @var $form CActiveForm */

<div class="form-horizontal well">
	<form method="post" enctype="multipart/form-data">
		<input type="file" name="archive"/>
		<input type="submit" name="enviar" value="Actualizar" class="btn btn-primary btn-large"/>
</div><!-- form -->

If is there any doubt.... or my post is not clear enough just tell me... Good luck

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users