Opción upload para autores

//En index.ctp:

 <?php echo $this->Html->link(__('Upload'), array('action' => 'upload', $autor['Autor']['id'])); ?>

//En el controlador

 public function upload($id = null) {
 if (!$this->Autor->exists($id)) {
 throw new NotFoundException(__('Invalid autor'));
 }
 if ($this->request->is(array('post', 'put'))) {
 

 $archivo = $this->request->data['Autor']['archivo'];
 move_uploaded_file($archivo['tmp_name'], WWW_ROOT . 'autores/' . $id . "/" . $archivo['name']);


 $this->Flash->success(__('Archivo subido.'));
 return $this->redirect(array('action' => 'index'));
 
 } else {
 $options = array('conditions' => array('Autor.' . $this->Autor->primaryKey => $id));
 $this->set('autor', $this->Autor->find('first', $options));
 }
 }

//La vista (entera)

<h2>Subiendo archivos para el autor <?=h($autor['Autor']['name'])?></h2>
<div class="autors form">
 
 <?php echo $this->Form->create('Autor', array('type' => 'file')); ?>
 <fieldset>
 <legend><?php echo __('Escoja el archivo a subir'); ?></legend>
 <?php
 
 echo $this->Form->file('archivo');
 ?>
 </fieldset>
 <?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
 <h3><?php echo __('Actions'); ?></h3>
 <ul>

 <li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Autor.id')), array('confirm' => __('Are you sure you want to delete # %s?', $this->Form->value('Autor.id')))); ?></li>
 <li><?php echo $this->Html->link(__('List Autors'), array('action' => 'index')); ?></li>
 </ul>
</div>

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos