title: Drupal 8: The file could not be created
body: <p>I have below code to upload excel file provided by API in base64_encode format. </p>
  
  <p>I would like to download that file and save in to file system.</p>
  
  <pre><code>public function import(Request $request) {
  
      if ($request-&gt;getMethod() === 'POST') {
        $data = json_decode($request-&gt;getContent(), TRUE);
  
        $directory = 'public://import/';
  
        // create directory if it does not exist
        file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  
        $uri = $directory. $data['name'];
        $files_data = preg_replace('#^data:application/\w+;base64,#i', '', $data['data']);
        $file_data = base64_decode($files_data);
        $file = file_save_data($file_data, $uri, FILE_EXISTS_RENAME);
        echo $file-&gt;id();
      }
  } 
  </code></pre>
  
  <p>It goes to below function and gives error, although I have created <code>tmp</code> folder on project root and given needed 777 permission.</p>
  
  <p><strong>/drupal/web/core/includes/file.inc</strong></p>
  
  <pre><code>function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
    // Write the data to a temporary file.
    $temp_name = drupal_tempnam('temporary://', 'file');
    if (file_put_contents($temp_name, $data) === FALSE) {
      \Drupal::messenger()-&gt;addError(t('The file could not be created.'));
      return FALSE;
    }
  
    // Move the file to its final destination.
    return file_unmanaged_move($temp_name, $destination, $replace);
  }
  </code></pre>
  
  <p>What I am missing? It is not converting and saving file.</p>