CakePHP 3模型表单使用用户提交的附件发送电子邮件

时间:2017-04-24 01:31:29

标签: cakephp

这些是我调试时的结果($ this-> request-> data)

我省略了无关紧要的字段

我明白了

[
'file' => 'Atest.doc'
]

我想要这个

[
'file' => [
    'tmp_name' => '/Applications/MAMP/tmp/php/phpgBEFye',
    'error' => (int) 0,
    'name' => 'Atest.doc',
    'type' => 'application/msword',
    'size' => (int) 45056
]

这是.ctp

 echo $this->Form->create($contact, ['id'=>'contact-form']);
 echo $this->Form->file('file', ['label'=> 'Cover Letter']);
 echo $this->Form->button('Send');
 echo $this->Form->end(); 

我的联系人控制器

 <?php
 namespace App\Controller;
 use App\Controller\AppController;
 use App\Form\ContactForm;
 use Cake\ORM\TableRegistry;
 use Cake\Mailer\Email;
 use Cake\Network\Exception\SocketException;

 use Burzum\FileStorage\Storage\StorageManager;

public function index($option = 'index'){
       $contact = new ContactForm();
       if ($this->request->is('post')) {


           if ($contact->execute($this->request->getData())) {
            //debug($this->request->getData() );
            }
        } 
  ///..I omitted the rest
}

1 个答案:

答案 0 :(得分:0)

echo $this->Form->create($document, ['enctype' => 'multipart/form-data']);