Save a managed file with cURL
file_save_data() will take the contents of a file and place into a location specified by a file URI. In your example, you can take your downloaded test.jpg, retrieve its contents, and have file_save_data() do the rest.

// Assuming same cwd where curl &amp; fopen was executed.
$image_file = file_save_data(file_get_contents('test.jpg'), 'public://test.jpg', FILE_EXISTS_REPLACE);

// test.jpg will be placed/overwritten into your managed file system.
// $image_file contains the Drupal file object.