CakePHP:文件替换不起作用

时间:2014-11-12 10:23:30

标签: php cakephp file-upload

我对cakephp非常新,我尝试使用文件替换来创建编辑功能,但它不起作用。如果文件已经存在,我会收到一条错误消息。

这是我的admin_edit.ctp代码:

<td>
  <?php if (!empty($this->data['Stock']['filepath'])): ?>
    <div class="input">
        <label>Uploaded File</label>
        <?php
                echo $this->Form->input('filepath', array('type'=>'hidden', 'label' => false));
                echo $this->Html->link(basename($this->data['Stock']['filepath']),
                     $this->data['Stock']['filepath']);
        ?>
    </div>
    <?php else: ?>
          <?php echo $this->Form->input('filename',array('type' => 'file', 'label' => false)); ?>
    <?php endif; ?>
</td>

这里有stock.php验证码 public $ validate = array(

        'filename' => array(
                // http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::uploadError
                'uploadError' => array(
                        'rule' => 'uploadError',
                        'message' => 'Something went wrong with the file upload - filename error',
                        'required' => FALSE,
                        'allowEmpty' => TRUE,
                ),
                // http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::mimeType
                'mimeType' => array(
                        'rule' => array('mimeType', array('image/gif','image/png','image/jpg','image/jpeg')),
                        'message' => 'Invalid file, only images allowed',
                        'required' => FALSE,
                        'allowEmpty' => TRUE,
                ),
                // custom callback to deal with the file upload
                'processUpload' => array(
                        'rule' => 'processUpload',
                        'message' => 'Something went wrong processing your file - process error',
                        'required' => FALSE,
                        'allowEmpty' => TRUE,
                        'last' => TRUE,
                )

processUpload:

public function processUpload($check=array()) {
    // deal with uploaded file
    if (!empty($check['filename']['tmp_name'])) {

        // check file is uploaded
        if (!is_uploaded_file($check['filename']['tmp_name'])) {
            return FALSE;
        }

        // build full filename

        $filename = WWW_ROOT . $this->uploadDir . DS . Inflector::slug(pathinfo($check['filename']['name'], PATHINFO_FILENAME)).'.'.pathinfo($check['filename']['name'], PATHINFO_EXTENSION);

        // @todo check for duplicate filename

        // try moving file
        if (!move_uploaded_file($check['filename']['tmp_name'], $filename)) {
            return FALSE;

            // file successfully uploaded
        } else {
            // save the file path relative from WWW_ROOT e.g. uploads/example_filename.jpg
            $this->data[$this->alias]['filepath'] = str_replace(DS, "/", str_replace(WWW_ROOT, "", $filename) );
        }
    }

    return TRUE;
}

错误消息:&#34;文件上传出现问题 - 文件名错误&#34;

保存之前:

public function beforeSave($options = array()) {
    // a file has been uploaded so grab the filepath
    if (!empty($this->data[$this->alias]['filepath'])) {
        $this->data[$this->alias]['filename'] = $this->data[$this->alias]['filepath'];


                    foreach (array_keys($this->hasAndBelongsToMany) as $model){
                    if(isset($this->data[$this->name][$model])){
                            $this->data[$model][$model] = $this->data[$this->name][$model];
                            unset($this->data[$this->name][$model]);
                    }
                }
    }

    return parent::beforeSave($options);

有人帮我找错了吗? 谢谢

0 个答案:

没有答案