title: PHP - Download and save file to specific directory
body: <p>I have a code that downloads a file with data that I am exporting to .csv format but it does not save it into specific directory in my Symfony project that I defined.</p>
  
  <p>Any help?</p>
  
  <pre><code>$response = new StreamedResponse();
      $response-&gt;setCallback(function () use ($results) {
          $handle = fopen('php://output', 'r+');
          $rows= $this-&gt;get('my.service')-&gt;getMyData();
          foreach($rows as $row) {
              fputcsv($handle, $row);
          }
          fclose($handle);
      });
  
      $fileName = 'user_' . date('Y-m-d') . '.csv';
      $rootDir = $this-&gt;container-&gt;get('kernel')-&gt;getRootDir();
      $dir = $rootDir . '/../var/upload/' . $fileName;
  
      $response-&gt;headers-&gt;set('Content-Type', 'application/force-download');
      $response-&gt;headers-&gt;set('Content-Disposition', 'attachment; filename='. $fileName);
  
      return $response;
  </code></pre>