Home > Enterprise >  How to get the content of the file that the user has uploaded in the form
How to get the content of the file that the user has uploaded in the form

Time:01-13

I have a form where the user can upload a file. The file should not be stored in my database.I want to view the contents of a file and then return a response to the user.How can i get the content. If I call by name, it just returns the file name.

  <input type="file" required  accept=".xml"id="file" name="file" multiple>
 public function loadData(Request $req) {

        dd($req->input('file'));
}

CodePudding user response:

You can get the contents of the file uploaded using file_get_contents() method. Example:

file_get_contents($request->file);

CodePudding user response:

From Laravel documentation:

You may retrieve uploaded files from an Illuminate\Http\Request instance using the file method or using dynamic properties. The file method returns an instance of the Illuminate\Http\UploadedFile class, which extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file.

public function loadData(Request $req)
{
    $content = $req->file('file')->get();
}
  • Related