how to create file programmatically only login users download it I want to create file programmatically ,I want only authenticated user can download it and direct access return  403 to them.
I search and saw How Can I Make File Downloads Available Only to Authenticated Users? but my files not field files(not record in file table) and programmatically I created them with:

    $temp1=variable_get('file_public_path').'/export';
      if(!file_prepare_directory($temp1,FILE_CREATE_DIRECTORY)){
          drupal_set_message(t('Problem in write file in directory'),'error');
          return;
      }
    $filename='Export-giftcard-'.time().'.csv';
        $filepath=$temp1.'/'.$filename;
    //    $filepath='sites\\defauls\\files\\Export-giftcard-.csv';
        $df = fopen($filepath, 'w');
        if(!$df){
            drupal_set_message(t('Can not write file in').$filepath,'error');
            return;
        }
        fputcsv($df,array('code','credit'));
   fclose($df);
    $path = file_create_url($filepath);
    drupal_set_message(t('Export done. ') . '&lt;a href="' . $path . '"&gt;' . t('Download export file') . '&lt;/a&gt;');


after rooby answer I update my question

I set private file system to system/files/export and  in my code use it 
$temp1 = variable_get('file_private_path');
also I implement hook_file_download

function mymodule_file_download($uri) {
  // Check if the file is controlled by the current module.
  if (!file_prepare_directory($uri)) {
    $uri = FALSE;
  }
// I will put my conditions later
      $info = image_get_info($uri);
      return array('Content-Type' =&gt; $info['mime_type']);
}


but when I try to click on link to download file return Forbidden to me.I think mymodule_file_download not run when I try to access system/files/export/myfile.csv
Create a private file field as you would if they were regular private files.

Hook in to the file download function: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_file_download/7

Let Drupal handle permissions,  create your file dynamically.