上传文件使用brandonsavage /上传到数据库

时间:2019-05-14 19:38:09

标签: database file-upload slim-3

使用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

0 个答案:

没有答案