Mohamed Midou
Mohamed Midou

Reputation: 29

Save File With Original name

Hi guys New To laravel here !! I want to save files inside my DB with their Original File Name !!

This is How I am Storing my File

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

}

How do i get to store the Original File Name inside My Database ? Thanks in Advace

Upvotes: 0

Views: 446

Answers (2)

albus_severus
albus_severus

Reputation: 3712

 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
     } 

  }


Upvotes: 0

teeyo
teeyo

Reputation: 3755

You can access all your file attributes using :

$file = $request->file('file');

What you are looking for is :

$file->getClientOriginalName();

And you have other attributes of course :

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

Try :

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

}

Upvotes: 2

Related Questions