Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deekepMaks's avatar

hasFile

Can I somehow use a filename I don't know? It can be image_1 image_2 and so on.

This does not work:

$request->hasFile('image_*');
0 likes
5 replies
tykus's avatar

Filename? Or file input name? Do you not own the form that is submitting files to your application?

deekepMaks's avatar

@tykus Yes, but this form has an array of files. When a user deletes a file, I delete exactly the location of the array, without shifting it, and then I generate a name by image_index in the array. That is, if there were 5 photos, the user deleted the first one, then only photos with indices 2-5 will come

tykus's avatar
tykus
Best Answer
Level 104

@deekepMaks in that case, you can get all of the uploaded files using $request->allFiles() - but this is a little blunt because you don't know the original input name.

You could instead have named the file inputs as an array; e.g.

<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">

Now you know the key images - and the items in the array will be accessed using $request->files('images') regardless the number.

If needed (whenever adding or removing), you can include the index for each input (similar to your current solution, e.g.

<input type="file" name="images[2]">

Please or to participate in this conversation.