jaahvicky
jaahvicky

Reputation: 448

How to save file details into DB in Laravel 5.1

I am trying to insert the details of an uploaded file into a database table and I am getting the following error:

Fatal error: Call to undefined method Symfony\Component\Finder\SplFileInfo::getClientOriginalName()

How would I get the getClientOriginalName(), getClientOriginalName() and getFilename() of a file in Laravel5?

Below is the code I am using.

public function add() 
{
  $directory = public_path('xml');

  $files = File::allFiles($directory);
  foreach ($files as $file) {
    $entry = new Xmlentry();
    $entry->mime = $file->getClientMimeType();
    $entry->original_filename = $file->getClientOriginalName();
    $entry->filename = $file->getFilename().'.'.$extension;
    $entry->save();
  }
}

Upvotes: 0

Views: 896

Answers (1)

avip
avip

Reputation: 1465

I'm a bit confused why you have getClientOriginalName() in there because that's aimed at temporary file names that have been uploaded but File::allFiles() is getting files from a directory that already have fixed names.

In addition to my comments above, I wanted to add you can just use the SplFileInfo methods.

I've taken the liberty of removing original file name from the code and correcting the lack of assignment statement for the variable $extension.

To answer your question:

public function add() 
{
  $directory = public_path('xml');

  $files = File::allFiles($directory);
  foreach ($files as $file) {
    $entry = new Xmlentry();
    $entry->mime = $file->getType();
    $entry->filename = $file->getFilename(). '.' . $file->getExtension();
    $entry->save();
  }
}

Upvotes: 1

Related Questions