How to create Google Facebook feed in magento2 root directory

<?php
ini_set("memory_limit", "4G");
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$obj->get('\Magento\Framework\App\State')->setAreaCode('frontend'); // for remove Area code is not set error
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$baseUrl = $storeManager->getStore()->getBaseUrl();
$heading = ['id', 'title', 'description', 'google_product_category', 'product_type', 'link', 'image_link', 'condition', 'availability', 'price', 'sale_price', 'gtin', 'brand', 'gender', 'age_group', 'color', 'size'];
$outputFile = "/srv/users/serverpilot/apps/serverpilot/public/pub/media/Productsfeed.csv";
$handle = fopen($outputFile, 'w');
fputcsv($handle, $heading);
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()->addAttributeToSelect('*')->addAttributeToFilter('status', '1')->load();
foreach ($collection as $product) {
    if ($product->getTypeId() == "configurable") {
        $helperImport = $_objectManager->get('\Magento\Catalog\Helper\Image');
        $product_imageUrl = $helperImport->init($product, 'product_page_image_small')->setImageFile($product->getSmallImage()) // image,small_image,thumbnail
        ->resize(380)->getUrl();
        //$category_name = "";
        $product_categories = "";
        $categoryIds = $product->getCategoryIds();
        $categoryCollection = $_objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
        foreach ($categoryIds as $catid) {
            $categories = $categoryCollection->create()->addAttributeToSelect('*')->addAttributeToFilter('entity_id', $catid);
            foreach ($categories as $category) {
                if ($category->getId() == 664 || $category->getId() == 673 || $category->getId() == 682 || $category->getId() == 686 || $category->getId() == 711 || $category->getId() == 725 || $category->getId() == 755 || $category->getId() == 765) {
                    $product_categories = $category->getName();
                    break;
                }
            }
            //$product_categories = implode(',', $category_name);
            
        }
        $availability = $product->getData('quantity_and_stock_status');
        if ($availability == 1) {
            $availability = "In stock";
        } else {
            $availability = "Out of stock";
        }
        $brand = $product->getResource()->getAttribute('brand')->getFrontend()->getValue($product);
        $gender = $product->getResource()->getAttribute('gender')->getFrontend()->getValue($product);
        if ($gender == 'Kids') {
            $gender = 'unisex';
        }
        $row = [$product->getId(), $product->getName(), strip_tags($product->getDescription()), $product_categories, $product_categories, $product->getProductUrl(), $product_imageUrl, 'new', $availability, $product->getData('price'), $product->getData('special_price'), '', $brand, $gender, '', '', ''];
        fputcsv($handle, $row);
    }
}
chmod('/srv/users/serverpilot/apps/serverpilot/public/pub/media/Productsfeed.csv', 0777);
if (file_exists($outputFile)) {
    //set appropriate headers
    header('Content-Description: File Transfer');
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename=' . basename($outputFile));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($outputFile));
    //ob_clean();flush();
    readfile($outputFile);
}

Comments