使用原始名称保存文件

时间:2019-11-13 14:38:00

标签: laravel

嗨,大家好,laravel在这里!我想使用原始文件名将文件保存在数据库中!

  

这就是我存储文件的方式

    public function store(Request $request)
{
    if ($request->hasFile('file')){
    $filepath=$request->file->store('uploads','public');
    $FileData=explode('/', $filepath);
    $file_name=$FileData[1];
    $offer->file=$file_name;
    } 

}

如何将原始文件名存储在数据库中?感谢Advace

2 个答案:

答案 0 :(得分:2)

您可以使用:

访问所有文件属性
$file = $request->file('file');

您正在寻找的是:

$file->getClientOriginalName();

当然还有其他属性:

// File extension.  
$file->getClientOriginalExtension();
// File Real Path
$file->getRealPath();
// File Size
$file->getSize();
// File Mime Type
$file->getMimeType();

尝试:

public function store(Request $request)
{
    if ($request->hasFile('file')) {
        $file = $request->file('file');
        $filename = $file->getClientOriginalName();
        // store the file
        $file->storeAs("uploads", $filename);
       // ...
   }

}

答案 1 :(得分:0)

 use Illuminate\Support\Facades\File;

 public function store(Request $request)
 {
    if ($request->hasFile('file'))
    {
        $path=your_folder_name;


       //it's for if your file not created already.then it will create your file
        if (!File::exists($path)) {
            mkdir(public_path($path), 0777, true);
        }


            $file_real_name=$file->getClientOriginalName();
            $file_extension_Name =  $file->getClientOriginalExtension();

            $file_name=$file_real_name.$file_extension_Name;
            $file->move(public_path($path), $file_name);
            $real_path = $path . $file_name;
            //if you want to save only the real name then save only $file_name
            //if you want to save into database with path then save $real_path
     } 

  }