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 Rate Topic: -----

#1 User is offline   Ani 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 26-September 12

Posted 23 January 2013 - 02:44 AM

I want to upload a file using Chtml in Yii. Table have 3 fields( id, name, attachment). In the index.php had following code.
<form>
    <?php $name='abcd'; ?>
    <?php echo CHtml::textArea('Text', '',array('maxlength' => 300, 'rows' => 4, 'cols' => 30)); ?>
    <?php  echo CHtml::button('Send', array('submit' => array('site/contact','name'=>$name)));  ?>
</form>


Controller action
public function actionContact()
        {
            $model= new Message;
            $name=$_REQUEST['name'];
            if(isset['$_REQUEST['name'])
            {
               $model->name=$name;
               $model->save();
            }
         }


I want to add file attachment to this form. How save the file name in database field and file in folder using chtml?
0

#2 User is online   KonApaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 522
  • Joined: 21-February 11
  • Location:Greece

Posted 23 January 2013 - 07:04 AM

Hi Rvr101

according to
http://www.yiiframew...-using-a-model/

You can do almost that you want

The extra code you have to write is the save of file name in database by below code


$model->nameImage = $model->image; //add this code

if($model->save())
     {....


obviously you have to add the extra field named 'nameImage' in your AR model
0

#3 User is offline   Ani 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 26-September 12

Posted 23 January 2013 - 07:42 AM

How upload a file without $model in the form?
0

#4 User is online   KonApaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 522
  • Joined: 21-February 11
  • Location:Greece

Posted 23 January 2013 - 08:38 AM

View PostRvr101, on 23 January 2013 - 07:42 AM, said:

How upload a file without $model in the form?



Using pure php code :)
http://www.tizag.com.../fileupload.php
0

#5 User is offline   Ani 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 26-September 12

Posted 23 January 2013 - 11:10 AM

View PostKonApaz, on 23 January 2013 - 08:38 AM, said:



In Yii how it possible?
0

#6 User is offline   PrplHaz4 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 92
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 23 January 2013 - 05:34 PM

View PostRvr101, on 23 January 2013 - 11:10 AM, said:

In Yii how it possible?


http://www.yiiframew...ileField-detail

Quote

Generates a file input. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'. After the form is submitted, the uploaded file information can be obtained via $_FILES[$name] (see PHP documentation).

0

#7 User is offline   Anthony Gough 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 29-October 12

Posted 23 January 2013 - 07:09 PM

Yii allows you to upload a file and store the file name in your database fairly simply. In your view make sure you include 'multipart/form-data' - example view
<?php /** @var BootActiveForm $form */
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id'=>'verticalForm',
    'htmlOptions'=>array('class'=>'well','enctype' => 'multipart/form-data'),
)); ?>

<div class="row">
		<?php echo $form->labelEx($model,'file_name'); ?>
		<?php echo CHtml::activeFileField($model,'file_name', array("style"=>"color:green;")); ?>
		<?php echo $form->error($model,'file_name'); ?>
</div>

Then in your controller you can use something like
public function actionCreate()
	{
		$model=new NewTask;
		$dir = Yii::getPathOfAlias('application.uploads');
		if(isset($_POST['NewTask']))
		{
			$model->attributes=$_POST['NewTask'];
			$model->file_name=CUploadedFile::getInstance($model,'file_name');
			$nf = $model->file_name;
			if($model->save())
			{
				$model->file_name->saveAs($dir.'/'.$fn);
				$model->file_name = $fn;
				$model->save();
........

Obviously you need to set up your database table (in this example file name is stored in attribute file_name). So in your model you can set some rules - in this example I only allow upload of xml files
public function rules()
	{
		return array(
			array('file_name', 'file', 'on'=>'insert',
				'types'=>'xml',),
			array('file_name', 'file', 'on'=>'update',
				'allowEmpty'=>true,
				'types'=>'xml',),
.....

Read CUploadedFile
0

#8 User is offline   Ani 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 26-September 12

Posted 24 January 2013 - 01:51 AM

Thanks for all rplys.
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