douduiti3040 2014-03-19 15:47
浏览 29
已采纳

数据名称未使用CakePHP Form保存到db

I am making an admin tool, for the site I am making, that allows the admin to make image uploads to a folder at the server, that stores images for a gallery. The file is being uploaded correctly but the image name isn't being placed on the database. The name should be placed at the table "gallery_images", on the "path" field. How can this be fixed?

I am using CakePHP 2.4.4

Controller

    <?php
class AdminsController extends AppController{

    public $components = array('RequestHandler');
    public function admin_index(){
        if(!$this->Session->check('Admin')){
        $this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
        $this->redirect(array(
                                'controller' => 'admins',
                                'action' => 'login'));
        }
        $this->layout='admin_index';
    }
    public function add_foto() {            
        if(!$this->Session->check('Admin')){
        $this->Session->setFlash('Está a aceder a uma zona restrita. Por favor faça Login.');
        $this->redirect(array(
                                'controller' => 'admins',
                                'action' => 'login'));
        }
        $this->layout='admin_index';
        $file=$this->request->data['gallery_images']['path'];
        if($this->request->is('post') || $this->request->is('put')){
                $this->Admin->create();
            $this->Admin->save($file);
            move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp_app/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);
                     if($this->Admin->save($this->request->data)){
                     $this->Session->setFlash(__('Ficheiro carregado com sucesso!'));
            }
        }
        //$this->Admin->id = $id;
        //$this->Post->save($data=array($this->data['Admins']['path']), $params=array('fieldList'=>'path'));
        //$this->Post->saveField('path', $this->data['Admins']['path']);
            /*if ($this->ModelName->save($this->request->data)) {
                $this->Session->setFlash('Data Saved!');
            }*/
        //if($this->request->is('post')){
        //  $this->Admin->save($this->request->data);
            //}
        //}
    }
}
    ?>

View

    <h2>Adicionar Fotografia</h2>
    <?php
echo "<br>";
echo $this->Form->create('Admin',array('type'=>'file'));
echo $this->Form->file('gallery_images.path');
echo "<br>";
//echo $this->Form->submit();
echo $this->Form->end('Guardar');
//validação é feita no AdminsController
    ?>
  • 写回答

2条回答 默认 最新

  • douaoren4402 2014-04-03 09:37
    关注

    Solved

    Controller

        <?php
        class GalleryController extends AppController {
    
        public function admin_upload_image(){
            $this->layout = 'admin_index';
            if($this->request->is('post') || $this->request->is('put')) {
              /*  $file = $this->request->data['gallery_images']['path']['name'];*/
            $file = array(
                    'GalleryImage' => array(
                    'path' => $this->request->data['gallery_images']['path']['name']
                                            )
                    );
                move_uploaded_file($this->data['gallery_images']['path']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/html/PushUp_app/app/webroot/img/gallery/' . $this->data['gallery_images']['path']['name']);
    
                $this->loadModel('GalleryImage');
                $this->GalleryImage->create();
    
                if($this->GalleryImage->save($file)){
                    $this->Session->setFlash(__('Ficheiro carregado com sucesso!'));
                }
                else{
                    $this->Session->setFlash(__('Erro ao carregar o ficheiro!'));
                }
            }
        }
    }
    ?>
    

    View

        <h2>Adicionar Fotografia</h2>
        <?php
    echo "<br>";
    echo $this->Form->create('GalleryImage',array('type'=>'file'));
    echo $this->Form->file('gallery_images.path');
    echo "<br>";
    //echo $this->Form->submit();
    echo $this->Form->end('Guardar');
    
        ?>
    

    Model

        <?php
    App::uses('AppModel', 'Model');
    class GalleryImage extends AppModel{
        public $displayField ='path';
        public $useTable = 'gallery_images';
    }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?