Programmatically Saving File

Get the filename URI path from the dir_name located under public files directory.
Save data in the destination. If file exists with the name, replace
Get the download link.

<?php
$dest
= file_build_uri('dir_name/'.$filename);
$file = file_save_data($data, $dest, FILE_EXISTS_REPLACE);
$download_link file_create_url($file->uri);
?>