简体   繁体   中英

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();
  }
}

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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM