If folders don't exist in URI path, file_save_data() fails
I Noticed Similar issue while saving files using 

 file_save_data();


In Drupal 8.

First I gave only first level directory to save the file as

$file = file_save_data($object-&gt;vertex_data, 'public://vertex_data.txt', FILE_EXISTS_REPLACE);


This worked fine.

But when I tried saving the file inside a subfolder as 

$file = file_save_data($object-&gt;vertex_data, 'public://objects/vertex_data.txt', FILE_EXISTS_REPLACE);


it gave me the following Error : 

File temporary://fileh1Gpl2 could not be moved/copied because the destination directory public://objects/ is not configured correctly.


The Solution for this is simple. Just use file_prepare_directory();

$directory = 'public://objects/'
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
$file = file_save_data($object-&gt;vertex_data,  $directory . 'vertex_data.txt', FILE_EXISTS_REPLACE);


This, first creates the folder if it does not exists and then proceeds to save the respective file.