简体   繁体   中英

Cannot even upload file onto temporary folder on web server with Cakephp

I am trying to upload an image file onto a web server using Cakephp 2.5.1.

I have a table called Upload and a column called picture .

Here is how my View code looks like;

<?php
echo $this->Form->create('Upload', array('type' => 'file'));
echo $this->Form->input('picture', array('type' => 'file'));
echo $this->Form->input('serial_no');
echo $this->Form->end('Submit');
?>

Here is how my controller code looks like;

   public function add() 
    {
        if ($this->request->is('post')) 
        {
            $this->Upload->create();
            if(!empty($this->request->data['Upload']['picture']['name']))
            {
                    $file = $this->request->data['Upload']['picture']; 
                    $ext = substr(strtolower(strrchr($file['name'], '.')), 1); 
                    $arr_ext = array('jpg', 'jpeg', 'gif', 'png'); 

                    //only process if the extension is valid
                    if(in_array($ext, $arr_ext))
                    {
                        $dest_file = WWW_ROOT . 'files' . DS .'uploads' . DS . 'test.jpg';
                        move_uploaded_file($file['tmp_name'], $dest_file);

                        //prepare the filename for database entry
                        $this->data['Upload']['picture'] = $file['name'];
                    }
            }     

            //Save filename into database
            if ($this->Upload->save($this->request->data, true)) {
                //$this->Session->setFlash(__('The upload has been saved.'));
                //return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The upload could not be saved. Please, try again.'));
            }
        } //if ($this->request->is('post')) 
    }

I discover that the file cannot even be uploaded onto temporary folder because the if statement if(!empty($this->request->data['Upload']['picture']['name'])) is never entered. Can someone advise what could possibly go wrong? Thank you.

Change

 if(!empty($this->request->data['Upload']['picture']['name']))

TO

 if($this->request->data['Upload']['picture']['name'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM