How to set original filename in symfony file upload

When you upload files in symfony, then save filename will be random. If you want set to original filename add to model class (where you want to upload files):

public function generateYourColumnNameForStoreFileFileName($file)
{
return $file->getOriginalName();
}

where YourColumnNameForStoreFile it`s your column name.

If you want your uploaded files to stay with the original file name (which is not randomly generated by symfony), just add following lines to the model class you want to upload files:

public function generateXxxFileName($file)
{
  return $file->getOriginalName();
}

where Xxx is the name of the column that stores the filename in the database model related table.

Leave a Comment: