Yii Framework Forum: How to make CMultiFileUpload work with CUploadedFile? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to make CMultiFileUpload work with CUploadedFile? Rate Topic: -----

#1 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 28 April 2009 - 03:12 PM

Anyone successfully put these two together?

I was trying to figure out a Yii way to handle file upload. The regular single file upload is well taken care of by Form and CFileUpload, however when I try to implement a multiple file upload using CMultiFileUpload and CUploadedFile, things become ugly.
I started with the simplest way, no form required, I have a view like


in controller, I use $_FILES['uploadfile'] to get the uploaded file array, which is fine. however, I couldn't find a way to pass to CUploadFile, which has only getInstance () and getInstanceByName() motheds, none of them seems fit the situation here. ( getInstance () only works with form, while getInstanceByName() seems only works with single upload).

I am not sure if I miss sth there, so any advice is welcome.

PS.: From I what I have learned, I think there is a misleading instruction in the CMultiFileUpload  doc http://www.yiiframew...MultiFileUpload.
Quote:

Quote

This is based on the jQuery Multi File Upload plugin. The uploaded file information can be accessed via $_FILES[widget-id], which gives an array of the uploaded files. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'.

actually, you can only access uploaded file information via $_FILES['inputfiled_name'], NOT $_FILES[widget-id].






0

#2 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 30 April 2009 - 12:49 AM

Anyone interested in this topic?
0

#3 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 30 April 2009 - 12:52 AM

Hi,
try this http://jquery-multif...ml#tab-Examples
This solution not base on CUploadedFile but work :)
0

#4 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 30 April 2009 - 03:30 AM

Maybe you will be interested on this: http://www.yiiframew...oc/cookbook/29/

I maked an example with multi image upload but you can adapt it on your own scenario :)
0

#5 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 30 April 2009 - 04:01 PM

thanks for your replies.
@qwerty : If you check the CMultiFileUpload class reference, you will see CMultiFileUpload is bridge class for jqueryMultipleFile plugin.

@StErMi : Though my original attempt is to make  CMultiFileUpload work with CUploadedFile, I gave the cookbook example a try, however it has many typos among other issues, I have left a comment there. Basically, I cannot get the file info in $_POST.
0

#6 User is offline   qwerty 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 290
  • Joined: 20-November 08

Posted 04 May 2009 - 05:51 PM

Hi,
you are right @will.

I use CMultiFileUpload and it works fine.

My view file:


...

<div class="form">
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

...

<?php $this->widget('CMultiFileUpload',array('name'=>'files', 'htmlOptions'=>array('class'=>'mf'), 'denied'=>Yii::t('mf','Nie można zazanczyć pliku'),'duplicate'=>Yii::t('mf','Ten plik jest już wstawiony'),'remove'=>Yii::t('mf','usuń'),'selected'=>Yii::t('mf','Zaznaczono plik: $file'),)); ?>


Controller file:

...

foreach($_FILES['files']['name'] as $key => $filename)
{
move_uploaded_file($_FILES['files']['tmp_name'][$key],$path.'/'.$filename);
}

...



I hope this help
2

#7 User is offline   will 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 21-March 09

Posted 04 May 2009 - 06:35 PM

Thank you qwerty, your approach has the work done.
However, I am still wondering if CMultiFileUpload can work with CUploadedFile. Maybe I should dig into the Yii core.
0

#8 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 05 May 2009 - 08:04 AM

I updated the cookbook with some fix anyway :)
0

#9 User is offline   edek 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 31-August 10

Posted 13 October 2010 - 01:43 PM

View Postwill, on 04 May 2009 - 06:35 PM, said:

Thank you qwerty, your approach has the work done.
However, I am still wondering if CMultiFileUpload can work with CUploadedFile. Maybe I should dig into the Yii core.


If you want to use CUploadedFile, I believe you can. Instead of qwerty's approach (of using move_uploaded_file),
you can use
$images = CUploadedFile::getInstancesByName('files'); //'files' is the name of the array (see qwerty's example)


or similar approach

in view:
<?php $this->widget('CMultiFileUpload',array('name'=>get_class($fileModel).'[files]',...

in controller:
<?php CUploadedFile::getInstances(get_class($fileModel),'files'); ?>


The class of $imageModel object does not need to have 'files' attribute (i think it wouldn't be of much use, unless you tweak model). I like this approach since files are uploaded inside $_FILES[model_class_here][files] and there is no bare 'files' key in $_FILES.
2

#10 User is offline   Vicente Russo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 22-February 11
  • Location:Londrina, Brazil

Posted 04 April 2011 - 06:18 AM

None of these methods worked for me (didn't tried move_uploaded.. because i think it works).

When i try

$images = CUploadedFile::getInstancesByName('files'); //'files' is the name of the array (see qwerty's example)


I have an empty array on $images. My input's name: "files[]"

I'm willing to make a very detailed post with examples if anyone helps me out!

Tks
The king is the man who can.
www.codare.com.br | @vicenterusso
0

#11 User is offline   maxazan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 14-January 12

Posted 02 April 2012 - 04:00 PM

View PostVicente Russo, on 04 April 2011 - 06:18 AM, said:

None of these methods worked for me (didn't tried move_uploaded.. because i think it works).

When i try

$images = CUploadedFile::getInstancesByName('files'); //'files' is the name of the array (see qwerty's example)


I have an empty array on $images. My input's name: "files[]"

I'm willing to make a very detailed post with examples if anyone helps me out!

Tks


May be your form widget does not have 'enctype' => 'multipart/form-data'

View:
<?php

$form = $this->beginWidget('CActiveForm', array(
    'id' => 'shop-items-form',
    'enableAjaxValidation' => false,
    'htmlOptions' => array(
        'enctype' => 'multipart/form-data')
        ));
?>

...

<?php

$this->widget('CMultiFileUpload', array(
    'name' => 'files',
    'accept' => 'jpeg|jpg|gif|png',
    'duplicate' => 'Duplicate file!',
    'denied' => 'Invalid file type',
));
?>

...

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


Controller:
$images = CUploadedFile::getInstancesByName('files');

0

#12 User is offline   Anas AbuDayah 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 27-February 11
  • Location:Amman, Jordan

Posted 05 May 2012 - 09:22 AM

great question thank you :)
@abudayah

للحصول على دعم أكثر يرجى متابعة هذه الصفحة على الفيسبوك
Posted Image

facebook.com/YiiArabia
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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