How to save uploaded file to specific folder location using Magento 2?

You can save any file type to custom folder location using Magento 2.

If you are working with image, pdf or CSV file and you want to save the file to a specific location using Magento standard way, You have to add a dependency to your __construct function.

TheĀ move_uploaded_file() function moves an uploaded file to a new location using core PHP but you can do it Using Magento standard way.

Magento\Framework\Filesystem
Magento\MediaStorage\Model\File\UploaderFactory

Both the class used to save the file to a specific location.

Your phtml file contains input type file,
<input id=”filename” name=”filename” class=”input-file required” type=”file” aria-required=”true”>

From server end, you just need to do below code to save your any custom file to folder,
I have given demo for Save CSV or Image to var/importexport folder using Block file.

In the above Block file, fileId is your input file name attribute.
Once you can run the above code, your file will be saved inside,
var/importexport/files.jpg or var/importexport/files.csv