Need help for upload file function.

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".

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>

	<div class="note_body">

		<?php if(Yii::app()->user->hasFlash('message')) echo Yii::app()->user->getFlash('message'); ?>

		<?php $form = $this->beginWidget(

		'CActiveForm',

    array(

        'id' => 'upload-form',

        'enableAjaxValidation' => false,

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

    )

);

		?>			

	<div class="form"><?php echo $form->errorSummary($model); ?></div>

		<table class="table_info">	

			<tr>

				<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>

			</tr>

		</table>

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

	</div>

</div>

then in my main page main.php i call that upload action with this code


<td>

					<a href="<?php echo Yii::app()->homeUrl; ?>admin/upload"><p>Items Upload</p>

					Items Upload</a>

				</td>

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 ?

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

Try this my friend




on the controller


	public function actionCreate()

	{

		$model=new Imagenes;


		$this->performAjaxValidation($model);


		if(isset($_FILES['archive']))

		{

			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']);

				$zip->close();

				unset($zip);


				unlink('imagenes/upload/'.$_FILES['archive']['name']);

			}


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


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

				$model->save();

			}


			$this->redirect(Yii::app()->user->returnUrl);

		}


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

			'model'=>$model,

		));

	}



your form:




<?php

/* @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"/>

		<br><br> 

		<input type="submit" name="enviar" value="Actualizar" class="btn btn-primary btn-large"/>

	</form>

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



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