Yii Framework Forum: File Upload - Yii Framework Forum

Jump to content

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

File Upload

#1 User is offline   JudithK 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 21-April 16

Posted 10 May 2016 - 11:47 AM

hello guys, my upload code is failing to send data to the database,my cod is as follows:

The Controller:

<?php
class UploadController extends Controller
{
function actionIndex()
{
$dir = Yii::getPathOfAlias('q5394727.uploads');
$uploaded = false;

$model=new Upload();

if(isset($_POST['Upload']))
{
$model->attributes=$_POST['Upload'];
$file=CUploadedFile::getInstance($model,'file');
if($model->validate()){
$uploaded = $file->saveAs($dir.'q5394727.upload'.$file->getName());

//$uploaded=$file->saveAs(Yii::app()->basePath.$fileName);
}
}

$this->render('index', array(
'model' => $model,
'uploaded' => $uploaded,
'dir' => $dir,
));
}
}



THE MODEL:

<?php
class Upload extends CFormModel
{
public $file;

public function rules()
{
return array(
array('file', 'file', 'types'=>'docx', 'allowEmpty'=>true, 'on'=>'insert,update'),
);
}
}

THE VIEW:

<?php
/* @var $this UploadController */

$this->breadcrumbs=array(
'Upload',
);
?>
<h1><?php echo $this->id . '/' . $this->action->id; ?></h1>
<?php if($uploaded):?>
<p>File was uploaded. Check <?php echo $dir?>.</p>
<?php endif ?>
<?php echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data'))?>
<?php echo CHtml::error($model, 'file')?>
<?php echo CHtml::activeFileField($model, 'file')?>
<?php echo CHtml::submitButton('Upload')?>
<?php echo CHtml::endForm()?>
<p>
You may change the content of this page by modifying
the file <tt><?php echo __FILE__; ?></tt>.
</p>



HOW AM I SUPPOSED TO SPECIFY THE PATH?
I TRIED THIS CODE ON THE LOCAL SERVER AND ON THE ONLINE SERVER. PLEASE HELP :(
0

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