Качване на файл в Cakephp 3.3

Опитвам се да запазя изображение в cakephp 3.0. Мога само да запазя името на файла в db, но не мога да съхраня действителния файл на сървъра. Нужда от помощ

форма:

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

Контролер за изображения:

 */
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']);
}

person amalik14    schedule 29.04.2015    source източник


Отговори (2)


Добре дошли в stackoverflow!

Моля, проверете този въпрос: cakePHP 3.0 качване на изображения

Това ще ви помогне, това е добър плъгин за качване на изображения: http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/

person Gilko    schedule 29.04.2015
comment
Този URL има вирусна атака... моля, не отваряйте други - person Mithilesh Kumar; 12.10.2018

За тези, които търсят отговора, просто променете този ред:

move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img' .DS. $file['name']);

DS е разделител на директория.

person Hanane    schedule 26.01.2016