Загрузить файл использовать brandonsavage / Загрузить в базу данных - PullRequest
0 голосов
/ 14 мая 2019

Использование фреймворка Slim3 и brandonsavage / Upload для загрузки файлов в папку - отлично работает - но когда я пытаюсь заставить его записать в базу данных, ничего не происходит.

Может ли кто-нибудь помочь мне выяснить, что не так - с моим кодом - когда я просто пишу в поле ввода, он пишет в БД.

Я бы хотел, чтобы имя файла загруженного файла было помещено в мое поле db-> table->, называемое billeder.

Мой контроллер выглядит так для обновления

public function update($slug,Request $request, Response $response, Router $router, Twig $view, text $text)
    {
        $var = 'billeder';
        $update = $text->where('id', $slug)->update([
            'overskrift' => $request->getParam('overskrift'),
            'tekst' => $request->getParam('tekst'),
            'billeder' =>  $text->upload($var),
            'link' => $request->getParam('link'),
        ]);

        return $response->withRedirect($router->pathFor('text.index'));
    }

А это модель для ввода данных в БД.

class text extends Model
{
    protected $table = 'webshoptekst';
    protected $foreignKey = 'slug';

    protected $fillable = [
        'overskrift',
        'tekst',
        'billeder',
        'filer',
        'linktekst',
        'navigationId',
        'navn',
      ];

    public static function txt() {

      return Text::get()->all();
    }

    public function Pages()
    {
        return $this->hasOne(Text::class, 'id', 'navn');
    }

    public function upload() {
      $storage = new \Upload\Storage\FileSystem('../public/uploads');
$file = new \Upload\File('billeder', $storage);

// Optionally you can rename the file on upload
$new_filename = uniqid('gk' . '-');
$file->setName($new_filename);

// Validate file upload
// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
$file->addValidations(array(
    // Ensure file is of type "image/png"
    // new \Upload\Validation\Mimetype('image/png'),

    //You can also add multi mimetype validation
    new \Upload\Validation\Mimetype(array('image/png', 'image/gif', 'image/jpg', 'image/jpeg', 'image/pdf')),

    // Ensure file is no larger than 5M (use "B", "K", M", or "G")
    new \Upload\Validation\Size('5M')
));

// Access data about the file that has been uploaded
$data = array(
    'name'       => $file->getNameWithExtension(),
    'extension'  => $file->getExtension(),
    'mime'       => $file->getMimetype(),
    'size'       => $file->getSize(),
    'md5'        => $file->getMd5(),
    'dimensions' => $file->getDimensions()
);

// Try to upload file
try {
    // Success!
    $file->upload();
} catch (\Exception $e) {
    // Fail!
    $errors = $file->getErrors();
}        
  }
}

```

I hope someone with Slim3 experience can find the error in my code and help me....

When I try just to put the data in by an input field in works and puts the data into the db - but I can't get the uploaded files name to get into the db
...