CakePHP -表单不提交文件字段


CakePHP - Form not submitting file field

我正在尝试上传一个图像文件到我的应用程序,用户可以选择通过上传图像来更改他的个人资料图片,但是当提交表单时,它不会将文件与表单一起提交。

我使用Meio Upload上传文件,我将粘贴我的视图控制器动作和模型:

视图:

<div class="prepend-1 span-8">
<?php
    echo $this->Form->create('Fonyker', array('action' => 'edit', 'class' => 'span-8', 'type' => 'file'));
?>
    <div class="span-3">
      <label for="FonykerImage" class="span-3 last">Profile Image</label>
      <div class="span-4 preview" style="border-color:black; border:dotted 2px; height:80px; width:80px">
        <img src="<?php echo $this->data['Fonyker']['image_url'];?>" style="width:80px;height:80px;"/>
      </div>
    </div>
    </br>
<?php
    echo $this->Form->input('image_url', array(
      'div' => array(
        'class' => 'span-5 last'
      ),
      'label' => array(
        'text' => 'Upload an image'
      ),
      'type' => 'file'
    ));
    echo $this->Form->input('username', array(
      'div' => array(
        'class' => 'span-8'
      ),
      'class' => 'input-text long',
      'label' => array(
        'text' => 'Username'
      ),
      'onsubmit' => 'return false;'
    ));
    echo $this->Form->input('email', array(
      'div' => array(
        'class' => 'span-8'
      ),
      'class' => 'input-text long',
      'label' => array(
        'text' => 'Email'
      ),
      'onsubmit' => 'return false;'
    ));
    echo $this->Form->input('name', array(
      'div' => array(
        'class' => 'span-8'
      ),
      'class' => 'input-text long',
      'label' => array(
        'text' => 'Name'
      ),
      'onsubmit' => 'return false;'
    ));
?>
        <div class="span-8 required">
            <label for="FonykerBirthdate">Birthdate</label>
            <input id="FonykerBirthdate" type="text" onsubmit="return false;" name="data[Fonyker][birthdate]" class="datepicker input-text long" enabled="false" value="<?php echo $this->data['Fonyker']['birthdate']; ?>">
        </div>
<?php
    $options = array('M' => 'M', 'F' => 'F');
    $attributes = array(
      'empty' => 'Gender',
      'class' => 'input-combo',
      'label' =>  array(
        'text' => 'Birthdate'
      )
    );
    echo $this->Form->select('gender', $options, NULL, $attributes);
    echo $this->Form->submit('Save', array(
      'class' => 'button medium blue',
      'id' => 'save-account-button'
    ));
?>
    <input id="FonykerId" type="hidden" name="data[Fonyker][id]" value="<?php echo $this->data['Fonyker']['id']?>">
<?php
    echo $this->Form->end();
?>
<div id="account-message" class="span-8" style="display: none;"></div>
</div>

行动:

function edit() {
      $this->layout = 'ajax';
      $this->autoRender = false;
      $this->Fonyker->recursive = -1;
      $response = null;
      if (!empty($this->data)) {
        $this->Fonyker->read(null, $this->data['Fonyker']['id']);
        pr($this->data);
        if ($this->Fonyker->save($this->data)) {
          $response = json_encode(array('ok' => true, 'msg' => 'Account information updated'));
        } else {
          $response = json_encode(array('ok' => false, 'msg' => 'Failed to update account'));
        }
      }

模型行为代码:

var $actsAs = array(
    'MeioUpload' => array( 
      'image_url' => array(
        'dir' => 'img/profile_pictures',
        'create_directory' => true,
        'allowed_mime' => array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'),
        'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif','.JPG'),
      )
    )
  ); 

上传工作在我的应用程序的另一部分,但不是在这种情况下,有什么想法吗?

      echo $response;
    }

问题是我通过AJAX提交表单,当我序列化表单时,文件不包含在其中,我是web开发新手,所以我不太了解一切的工作原理,但似乎你不能通过AJAX提交文件。

所以我修改了我的代码工作没有AJAX请求,只是提交表单与POST正常,它工作得很好。

以下是我要检查的几件事:

在您的控制器函数中,在保存之前,您可能会检查$this->data['image_url']的内容,并查看error字段是否为非零。PHP手册中列出了错误代码,可以为您提供问题的线索。

也可以在没有MIME类型限制的情况下进行测试。MeioUpload可能依赖于浏览器报告的MIME类型,而浏览器报告的MIME类型几乎肯定只基于文件名扩展名。我也知道浏览器(Safari,我想?)报告一切为application/octet-stream。(您也可以使用PHP的FileInfo,它根据文件内容确定MIME类型。)

从这里开始