Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
CakePHP (2.1) Media Plugin - Multi File Upload
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
double07  
View profile  
 More options Apr 15 2012, 8:42 pm
From: double07 <br...@bluenectar.com.au>
Date: Sun, 15 Apr 2012 17:42:25 -0700 (PDT)
Local: Sun, Apr 15 2012 8:42 pm
Subject: CakePHP (2.1) Media Plugin - Multi File Upload
Hi All,

I'm using the cakephp media plugin in my project using the monolithic
style attachments table, i.e. all the attachments go in the one table
with foreign_key, model, group etc. saved with the file details. So my
model looks like:

class ProjectProfile extends AppModel {

var $name = 'ProjectProfile';
var $useDbConfig = 'default';
var $useTable = 'project_profiles';
var $actsAs = array('Media.Transfer', 'Media.Generator');

public $belongsTo = array(
    'Project' => array(
        'className' => 'Project',
        'foreignKey' => 'pjID'
    )
);

var $hasMany = array(
      'Photo' => array(
          'className' => 'Media.Attachment',
          'order' => 'Photo.basename, Photo.id',
          'foreignKey' => 'foreign_key',
          'conditions' => array('Photo.model' => 'ProjectProfile',
'Photo.group' => 'Photo'),
          'dependent' => true)
  );

Then a saveAll in the controller when saving my record saves the
attached file(s).

This all works fine, however I'd really like to be able to upload
multiple files at once, which the plugin does support by doing this in
the form:

echo $this->Form->hidden('Photo.0.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.0.file', array('type' => 'file');
echo $this->Form->hidden('Photo.1.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.1.file', array('type' => 'file');
echo $this->Form->hidden('Photo.2.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.2.file', array('type' => 'file');

But I think you'd agree that's a bit cumbersome to have to click
browse for each individual file. The simplist method I could see to to
allow multiple file uploads was to use the HTML5 multiple file section
option - http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multip...
:

echo $this->Form->input('files.', array('type' => 'file',
'multiple'));

This allows you to shift click in the file browser to select multiple
files then puts the files into an array to save... however, this field
format isn't handled by the media plugin. Also, there'd be no way to
add the model, group etc. fields on the save as far as I could see.

So, does anybody know how I can handle multi file uploads with the
media plugin using the monolithic model? I'm open to all suggestions.

Thanks in advance.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paulo de Almeida  
View profile  
 More options Apr 16 2012, 7:36 am
From: Paulo de Almeida <paulodealme...@gmail.com>
Date: Mon, 16 Apr 2012 08:36:22 -0300
Local: Mon, Apr 16 2012 7:36 am
Subject: Re: CakePHP (2.1) Media Plugin - Multi File Upload

Try MeioUpload.

https://github.com/jrbasso/MeioUpload

2012/4/15 double07 <br...@bluenectar.com.au>

--
Paulo de Almeida

Linux User #494076
Ubuntu User # 28289

"In a world without walls who needs windows and gates?"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jeremyharris  
View profile  
 More options Apr 16 2012, 6:44 pm
From: jeremyharris <funeralm...@gmail.com>
Date: Mon, 16 Apr 2012 15:44:09 -0700 (PDT)
Local: Mon, Apr 16 2012 6:44 pm
Subject: Re: CakePHP (2.1) Media Plugin - Multi File Upload

You could try looping through the files array and saving them manually:

foreach $_FILES['files'] as $file {
// create data array containing fields such as a foreignKey, model, etc.
and save them as individual records

}

Or, instead of using the multiple option, you can try processing them each
as a single request so you don't have to change anything in your controller
but rather just how the view presents it. I've used drag and drop jQuery
plugins and the like to help with this. It's by far my favorite solution,
because it doesn't rely on browser specific features (such as HTML5
multiple upload).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
double07  
View profile  
 More options Apr 24 2012, 2:27 am
From: double07 <br...@bluenectar.com.au>
Date: Mon, 23 Apr 2012 23:27:08 -0700 (PDT)
Local: Tues, Apr 24 2012 2:27 am
Subject: Re: CakePHP (2.1) Media Plugin - Multi File Upload
Sorry I never got notified of replies for some reason, I will try this
looping method I think it could work.

Thanks!

On Apr 17, 7:44 am, jeremyharris <funeralm...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
double07  
View profile  
 More options Apr 30 2012, 11:09 pm
From: double07 <br...@bluenectar.com.au>
Date: Mon, 30 Apr 2012 20:09:33 -0700 (PDT)
Local: Mon, Apr 30 2012 11:09 pm
Subject: Re: CakePHP (2.1) Media Plugin - Multi File Upload
Hi Jeremy,

Not having much luck with the saving manually option, do you having
any working examples of drag and drop (jquery) I can look at?

Thanks,

-Brett

On Apr 17, 7:44 am, jeremyharris <funeralm...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jeremyharris  
View profile  
 More options May 1 2012, 10:27 am
From: jeremyharris <funeralm...@gmail.com>
Date: Tue, 1 May 2012 07:27:28 -0700 (PDT)
Local: Tues, May 1 2012 10:27 am
Subject: Re: CakePHP (2.1) Media Plugin - Multi File Upload

I don't have any templates, but I use file-uploader[1] on a currently
active project. It doesn't require jQuery but works pretty nicely.

It sends each upload as a separate request, so write the controller code as
if you're receiving one file (like the media plugin examples) and you
should be good. This method is preferred, because when javascript is
disabled the logic in the controller doesn't need to change will still work
(i.e., old school single upload).

1: https://github.com/valums/file-uploader  (doesn't seem to be maintained
by the author but there are numerous forks)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
double07  
View profile  
 More options May 9 2012, 10:26 pm
From: double07 <br...@bluenectar.com.au>
Date: Wed, 9 May 2012 19:26:49 -0700 (PDT)
Local: Wed, May 9 2012 10:26 pm
Subject: Re: CakePHP (2.1) Media Plugin - Multi File Upload
Well this whole project has made me realise how much I suck at Ajax/JS
etc. I ended up getting this working using cakephp's HTML5 multi file
support and converting that data array to one that the media plugin
would understand (as per Jeremy's first suggestion!).

In case anybody else is looking to do this...

In the view add to your form:
echo $this->Form->input('files.', array('type' => 'file',
'multiple'));

So that'll allow you to select multiple files.

In the controller do something like:
$i = 0;
foreach ($this->request->data['Model']['files'] as $file) {
$this->set($this->request->data['Photo'][$i]['model'] = 'Model');
$this->set($this->request->data['Photo'][$i]['group'] = 'group');
$this->set($this->request->data['Photo'][$i]['file'] = $this->request-

>data['Model']['files'][$i]);
$i++;
}

Then make sure you have saveAll:
if ($this->Model->saveAll($this->request->data)) { ...

I'm going to spend some time sorting it out better and making it as
reusable as possible. This solution is good for me as it's for an
intranet site so I have control over what browser people are using.
And if the users's browser doesn't support html5 then they can still
upload, just one file at a time.

Thanks for your suggestions Jeremy.

On May 1, 11:27 pm, jeremyharris <funeralm...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
double07  
View profile  
 More options May 14 2012, 8:46 pm
From: double07 <br...@bluenectar.com.au>
Date: Mon, 14 May 2012 17:46:24 -0700 (PDT)
Local: Mon, May 14 2012 8:46 pm
Subject: Re: CakePHP (2.1) Media Plugin - Multi File Upload
Just thought I'd post my slightly cleaner solution to this which makes
use of a modified version of the attachments element in the media
plugin. This allows you to use the attachment multiple times in a form
for when you have different groups of attachments i.e. photos, files,
videos etc. I'm no programmer, just a designer so there's probably a
better way to do it but I don't know it :)

To save me editing too much I'll just use my example of a
ProjectProfile model:

ProjectProfile.php >

<?php
App::uses('AppModel', 'Model');
class ProjectProfile extends AppModel {

        var $name = 'ProjectProfile';
        var $useDbConfig = 'default';
        var $useTable = 'project_profiles';
        var $actsAs = array('Media.Transfer',
                                                'Media.Generator'
                                                );

        public $belongsTo = array(
                'Project' => array(
                        'className' => 'Project',
                        'foreignKey' => 'pjID'
                        //'conditions' => '',
                        //'fields' => '',
                        //'order' => ''
                )
        );

        var $hasMany = array(
          'Photo' => array(
              'className' => 'Media.Attachment',
                          'order' => 'Photo.basename, Photo.id',
              'foreignKey' => 'foreign_key',
              'conditions' => array('Photo.model' => 'ProjectProfile',
'Photo.group' => 'Photo'),
              'dependent' => true),
                   'Attachment' => array(
              'className' => 'Media.Attachment',
                          'order' => 'Attachment.basename, Attachment.id',
              'foreignKey' => 'foreign_key',
              'conditions' => array('Attachment.model' =>
'ProjectProfile', 'Attachment.group' => 'Attachment'),
              'dependent' => true)
          );

}

ProjectProfilesControler.php >

<?php
App::uses('AppController', 'Controller');
class ProjectProfilesController extends AppController {

....

public function admin_edit($id = null) {
                $this->ProjectProfile->id = $id;
                if (!$this->ProjectProfile->exists()) {
                        throw new NotFoundException(__('Invalid project profile'));
                }
                if ($this->request->is('post') || $this->request->is('put')) {
                                        //convert cakephp html5 file upload data array to media plugin
required format
                                        $model = Inflector::classify($this->params['controller']);
                                        foreach ($this->request->data[$model]['assocAlias'] as
$assocAlias) {
                                          $i = 0;
                                          foreach ($this->request->data[$model][$assocAlias.'files'] as
$file) {
                                                  $this->set($this->request->data[$assocAlias][$i]['model'] =
$model);
                                                  $this->set($this->request->data[$assocAlias][$i]['group'] =
strtolower($assocAlias));
                                                  $this->set($this->request->data[$assocAlias][$i]['file'] =
$this->request->data[$model][$assocAlias.'files'][$i]);
                                                  $i++;
                                          }
                                        }
                        if ($this->ProjectProfile->saveAll($this->request->data)) {
                                $this->Session->setFlash(__('The project profile has been
saved'));
                                $this->redirect(array('action' => 'admin_edit', $id));
                        } else {
                                $this->Session->setFlash(__('The project profile could not be
saved. Please, try again.'));
                        }
                } else {
                        $this->request->data = $this->ProjectProfile->read(null, $id);
                }
                $projects = $this->ProjectProfile->Project->find('list',
array('fields' => array('Project.pjID', 'Project.pntitle')));
                $this->set(compact('projects'));
        }

....

?>

Then in the view/form:
admin_edit.php >
<div class="projectProfiles form">
<?php echo $this->Form->create('ProjectProfile',
array('type'=>'file'));?>
        <fieldset>
                <legend><?php echo __('Admin Edit Project Profile'); ?></legend>
        <?php
                echo $this->Form->input('id');
                echo $this->Form->input('pjID', array('label' => 'Project',
'options' => $projects));
                echo $this->Form->input('description');
                echo $this->Form->input('longitude');
                echo $this->Form->input('latitude');
                echo $this->Form->input('major', array('label' => 'Major Project'));
                //The num variable below is just used in the media
attachments element so the controller can loop through the array, so
just make sure you give each of the nums a sequential numerical order.
The assocAlias just sets the group of attachments.
                echo $this->element('Media.attachments', array('model' =>
'ProjectProfile', 'assocAlias' => 'Photo', 'num' => 0));
                echo $this->element('Media.attachments', array('model' =>
'ProjectProfile', 'assocAlias' => 'Attachment', 'num' => 1));
                ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>

Then this is a slightly modified version of the media attachment
element, replace with this version...
/Plugin/Media/View/Elements/attachment.php
<?php
if (!isset($this->Media) || !is_a($this->Media, 'MediaHelper')) {
        $message = 'Attachments Element - The media helper is not loaded but
required.';
        trigger_error($message, E_USER_NOTICE);
        return;

}

if (!isset($previewVersion)) {
        $previewVersion = 's';

}

/* Set $assocAlias and $model if you're using this element multiple
times in one form */

if (!isset($assocAlias)) {
        $assocAlias = 'Attachment';

} else {

        $assocAlias = Inflector::singularize($assocAlias);

}

if (!isset($model)) {
        $model = $this->Form->model();

}

$modelId = $this->Form->value($this->Form->model().'.id');

if (isset($this->request->data[$assocAlias][0]['basename'])) {
        array_unshift($this->request->data[$assocAlias],array());

}

if (!isset($title)) {
        //$title = sprintf(__('%s', true),
Inflector::pluralize($assocAlias));
        $title = Inflector::pluralize($assocAlias);

}

?>
<div class="attachments element">
        <?php echo $title ?>
        <!-- New Attachment -->
        <div class="new" id="<?php echo $assocAlias ?>">
        <?php
                echo $this->Form->hidden($model.'.assocAlias.'.$num, array('value'
=> $assocAlias));
                echo $this->Form->input($assocAlias.'files.', array(
                'type' => 'file', 'multiple',
                'error' => array(
                                'error'      => __('An error occured while transferring the
file.', true),
                                'resource'   => __('The file is invalid.', true),
                                'access'     => __('The file cannot be processed.', true),
                                'location'   => __('The file cannot be transferred from or to
location.', true),
                                'permission' => __('Executable files cannot be uploaded.', true),
                                'size'       => __('The file is too large.', true),
                                'pixels'     => __('The file is too large.', true),
                                'extension'  => __('The file has the wrong extension.', true),
                                'mimeType'   => __('The file has the wrong MIME type.', true),
                )));

        ?>

        </div>
    <?php

        ?>

        <!-- Existing Attachments -->
        <div class="existing">
        <?php if (isset($this->data[$assocAlias])): ?>
                <?php for ($i = 1; $i < count($this->data[$assocAlias]); $i++): ?>
                <div>
                <?php
                        $item = $this->data[$assocAlias][$i];

                        echo $this->Form->hidden($assocAlias . '.' . $i . '.id',
array('value' => $item['id']));
                        echo $this->Form->hidden($assocAlias . '.' . $i . '.model',
array('value' => $model));
                        echo $this->Form->hidden($assocAlias . '.' . $i . '.group',
array('value' => $item['group']));
                        echo $this->Form->hidden($assocAlias . '.' . $i . '.dirname',
array('value' => $item['dirname']));
                        echo $this->Form->hidden($assocAlias . '.' . $i . '.basename',
array('value' => $item['basename']));
                        echo $this->Form->hidden($assocAlias . '.' . $i . '.alternative',
array('value' => $item['alternative']));

                        if ($file = $this->Media->file("{$item['dirname']}/
{$item['basename']}")) {
                                $url = $this->Media->url($file);
                                $size = $this->Media->size($file);
                                $name = $this->Media->name($file);

                                echo $this->Media->embed($this->Media->file("{$previewVersion}/
{$item['dirname']}/{$item['basename']}"), array(
                                        'restrict' => array('image')
                                ));

                                if (isset($this->Number)) {
                                        $size = $this->Number->toReadableSize($size);
                                } else {
                                        $size .= ' Bytes';
                                }

                                printf(
                                        '<span class="description">%s&nbsp;(%s/%s) <em>%s</em></span>',
                                        $url ? $this->Html->link($item['basename'], $url) :
$item['basename'],
                                        $name,
                                        $size,
                                        $item['alternative']
                                );
                        }

                        echo $this->Form->input($assocAlias . '.' . $i . '.alternative',
array('label' => 'Description', 'value' => $item['alternative']));

                        echo $this->Form->input($assocAlias . '.' . $i . '.delete', array(
                                'label' => __('Release', true),
                                'type' => 'checkbox',
                                'value' => 0
                        ));
                ?>
                </div>
                <?php endfor ?>
        <?php endif ?>
        </div>
</div>

Hope that helps somebody.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »