How to import images from the external URL in Magento2

After Adding this code

    $storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface'); 
     $currentStore = $storeManager->getStore();

    $mediaUrl = $currentStore->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
    $storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface'); 
    $currentStore = $storeManager->getStore();
    $image_url  = $importData('addtional_image'); 
    $image_type = substr(strrchr($image_url,"."),1); 
    $filename   = md5($image_url .$image_type;
    $filepath   = $mediaUrl . $filename;
    file_put_contents($filepath, file_get_contents(trim($image_url))); 
    $mediaAttribute = array (
            'thumbnail',
            'small_image',
            'image'
    );

Facing the issue failed to open stream: HTTP wrapper does not support writeable connections