【问题标题】:Uploading Image and Saving Filepath to database Cakephp上传图片并保存文件路径到数据库 Cakephp
【发布时间】:2023-04-09 07:34:01
【问题描述】:

大家好,我正在尝试将图像保存到文件系统,然后将文件路径保存到数据库。

我的控制器

<?php

class ProductsController extends AppController {

    public function add() {
     if ($this->request->is('post')) {
     if (!empty($this->request->data)) {
     $this->Product->create();
     $this->Product->save($this->request->data['post']);
       }
     }  
    }
  }

我的查看代码:

    <div class="add_ondemand">
    <h1>Add Post</h1>
    <?php
    echo $this->Form->create('post');
    echo $this->Form->input('name', array('placeholder'=>'name'));
    echo $this->Form->textarea('description', array('placeholder'=>'description'));
    echo $this->Form->input('director', array('placeholder'=>'director'));
    echo $this->Form->input('cast', array('placeholder'=>'cast'));
    echo $this->Form->input('release_date', array('placeholder'=>'release      date','id'=>'datepicker', 'type'=>'text'));
    echo $this->Form->input('Product.File',array('type' => 'file'));
    echo $this->Form->end('Save Post');
    ?>

</div>



<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>

任何帮助将不胜感激

【问题讨论】:

    标签: database cakephp


    【解决方案1】:

    将此用于您的 Product.php 模型文件.......

    public $validate = array(
                        'photo_name' => array(
                            'uploadError' => array(
                                'rule' => 'uploadError',
                                'message' => 'Something went wrong with the file upload',
                                'required' => FALSE,
                                'allowEmpty' => TRUE,
                            ),
                            'photoSize' => array(
                                'rule' => array('fileSize','<=','2MB'),
                                'message' => 'Photo size must be less then 2MB.',
                                'required' => FALSE,
                                'allowEmpty' => TRUE,
                            ),
                            'mimeType' => array(
                                'rule' => array('mimeType', array('image/gif','image/png','image/jpg','image/jpeg')),
                                'message' => 'Invalid file, only images allowed',
                                'required' => FALSE,
                                'allowEmpty' => TRUE
                            ),
                            'processUpload' => array(
                                'rule' => 'processUpload',
                                'message' => 'Something went wrong processing your file',
                                'required' => FALSE,
                                'allowEmpty' => TRUE,
                                'last' => TRUE,
                            )
                        )
                );
    
    
    public $uploadDir = 'img/user_photos';
    
    /**
     * Process the Upload
     * @param array $check
     * @return boolean
     */
    public function processUpload($check=array()) {
        // deal with uploaded file
        if (!empty($check['photo_name']['tmp_name'])) {
    
            // check file is uploaded
            if (!is_uploaded_file($check['photo_name']['tmp_name'])) {
                return FALSE;
            }
    
            // build full filename
            $filename = WWW_ROOT . $this->uploadDir . DS . String::uuid().'.'.pathinfo($check['photo_name']['name'], PATHINFO_EXTENSION);
    
            // @todo check for duplicate filename
    
            // try moving file
            if (!move_uploaded_file($check['photo_name']['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.IMAGES_URL, "", $filename));
            }
        }
    
        return TRUE;
    }
    
    /**
     * Before Save Callback
     * @param array $options
     * @return boolean
     */
    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]['photo_name'] = $this->data[$this->alias]['filepath'];
        }       
        return parent::beforeSave($options);
    }
    
    public function beforeValidate($options = array()) {
        // ignore empty file - causes issues with form validation when file is empty and optional
        if (!empty($this->data[$this->alias]['photo_name']['error']) && $this->data[$this->alias]['photo_name']['error']==4 && $this->data[$this->alias]['photo_name']['size']==0) {
            unset($this->data[$this->alias]['photo_name']);
        }
    
        parent::beforeValidate($options);
    }
    

    这不是我的代码,我忘记了我得到它的来源。

    【讨论】:

    • 对我不起作用...不上传并且文件路径未发送到数据库
    • 您使用的是哪个 Cakephp 版本?有任何错误信息吗?尝试更改为 $uploadDir = 'img';
    • 我使用的是 2.4.6 稳定版,没有任何错误。该更改并没有改变,我认为这只是文件将存储的目录。由于路径没有被保存,我认为还有其他问题
    • 我知道了... 看,我没有为您提供仅用于复制粘贴的代码。您必须将它们更改为您的项目。我用于我的项目的 ['photo_name']... 你必须将它们全部更改为 ['File']。
    猜你喜欢
    • 1970-01-01
    • 2013-11-01
    • 2015-04-20
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多