kennylucia
(Kenny Lucia)
March 15, 2017, 12:00am
1
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;
if(isset($_POST['Upload']))
{
$model->attributes=$_POST['Upload'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs('/../themes/default/images/shopimg');
// 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> → 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 ?
tri
(tri - Tommy Riboe)
March 15, 2017, 5:19pm
2
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
reykel
(Rhpalacios66)
April 8, 2017, 12:48am
3
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