PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, April 18, 2022

[FIXED] How to get file input name in Laravel?

 April 18, 2022     jquery, laravel, php     No comments   

Issue

I am using a jQuery form builder to create any kind of form input. The issue I had, in particular, is when I create multiple file inputs, the input name is generated randomly by default, the format is something like "file-0000-0". I would like to get the input name but since it's random, I can only think of one way to fetch the name which is by using the allFiles() method. How do I fetch the name of the input?

Code Example

$fileRequest = $request->allFiles();

return $fileRequest;

It will return something like this:

{file-1649657296668-0: {…}, file-1649657297967-0: {…}}.

Now how do I get both of the file input names above? Thanks for the help.


Solution

Since allFiles() returns associative array you can get keys:

$files = $request->allFiles();
$name_of_files = array_keys($files);
// ["file-1649657296668-0", "file-1649657297967-0"]

Or you can loop through that array and access files as well:

$files = $request->allFiles();
foreach($files as $name_of_file => $file)  {
  echo $name_of_file; // file-1649657296668-0
  // $file is UploadedFile instance
}


Answered By - Malkhazi Dartsmelidze
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing