Datei-upload in Cakephp 3.3

Ich versuche zu speichern ein Bild in cakephp 3.0. Ich bin nur in der Lage, zum speichern der Dateinamen in der db, jedoch nicht zum speichern der tatsächlichen Datei auf dem server. Brauche Hilfe

Form:

        echo $this->Form->create('User', array('url' => array('action' => 'create'), 'enctype' => 'multipart/form-data'));
        echo $this->Form->input('upload', array('type' => 'file'));

Bilder-controller:

 */
public function add()
{
    $image = $this->Images->newEntity();
    //Check if image has been uploaded
    if(!empty($this->request->data['Images']['upload']['name']))
    {
        $file = $this->request->data['Images']['upload']; //put the data into a var for easy use

        $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
        $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions

        //only process if the extension is valid
        if(in_array($ext, $arr_ext))
        {
            //do the actual uploading of the file. First arg is the tmp name, second arg is
            //where we are putting it
            move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img' . $file['name']);

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

    if ($this->request->is('post')) {
        $image = $this->Images->patchEntity($image, $this->request->data);
        if ($this->Images->save($image)) {
            $this->Flash->success('The image has been saved.');
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error('The image could not be saved. Please, try again.');
        }
    }
    $this->set(compact('image'));
    $this->set('_serialize', ['image']);
}

InformationsquelleAutor amalik14 | 2015-04-29

Schreibe einen Kommentar