Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

How to keep uploaded filename and write filename to db #112

Open
Budolfsen opened this issue May 30, 2019 · 3 comments
Open

How to keep uploaded filename and write filename to db #112

Budolfsen opened this issue May 30, 2019 · 3 comments

Comments

@Budolfsen
Copy link

Hi
I am trying to use this on a project and it works fine with uploading the files with the unique id - but I was wondering how I can get it to use the uploaded files name and write it to the database.

This is my code (controller, text-model and imagemodel)

Text controller:

public function update($slug,Request $request, Response $response, Router $router, Twig $view, Text $text, Billede $billede)
    {
        
        $update = $text->where('id', $slug)->update([
            'overskrift' => $request->getParam('overskrift'),
            'tekst' => $request->getParam('tekst'),
            'link' => $request->getParam('link'),
            'billede_id' => $billede->id,
            ]);       
            $var = 'billede';
            $billede = $billede->firstOrCreate([
        //            'alt' => $request->getParam('alt'),
                 
                'billedenavn' =>  $billede->upload($var),
            ]);
             
            $billede->text()->save($update);
        print_r($new_filename);
}

My textmodel

<?php

namespace Cart\Models;

use Cart\Models\User;
use Cart\Models\Admin;
use Cart\Models\Text;
use Cart\Models\Billede;
use Upload\Storage\FileSystem;
use Upload\File;
use Upload\Validation\Mimetype;
use Upload\Validation\Size;
use Illuminate\Database\Eloquent\Model;

class Text extends Model
{
    protected $table = 'webshoptekst';
    protected $foreignKey = 'billede_id';

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

    public static function txt() {

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

My image model (billedemodel)

<?php

namespace Cart\Models;

use Cart\Models\Billede;
use Cart\Models\Text;
use Upload\Storage\FileSystem;
use Upload\File;
use Upload\Validation\Mimetype;
use Upload\Validation\Size;
use Illuminate\Database\Eloquent\Model;

class Billede extends Model
{
    protected $table = 'webshopbilleder';


    protected $fillable = [
        'billedenavn',
      ];

      public function text(){
        return $this->hasOne(Text::class);
    }  

    public function upload() {
    $storage = new \Upload\Storage\FileSystem('../public/uploads');
    $file = new \Upload\File('billede', $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();
    }        
  }

  public $timestamps = false;
}

It uploads the files as it should with uniq-id but I can't get it to write the name to the database.

Can anyone help - please

@iRaziul
Copy link

iRaziul commented Apr 26, 2020

What you can do is retrieve the filename first then setName.

Without original filename

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

With original filename

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

@gusbemacbe
Copy link

$original_filename = $file->getName();

I replaced $file->setName($new_filename) with $file->setName($original_filename), but it did not upload and did not overwrite the old images.

I need to upload the new image with the same name to overwrite the old image with the same name. I do not want to use unique ID.

@iRaziul
Copy link

iRaziul commented Apr 27, 2021

Maybe you are doing something wrong, Can you show the code snippet?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants