Search code examples
phpcron-taskbluehost

How to download attachement to the server using bluehost cron job


I would like to run a cron job executing php script. The purpose of the script is to create csv file with data selected from database and to save it on the server. This is a command I created:

php -c /home1/breezeea/public_html/php.ini /home1/breezeea/public_html/portaltest/ProductsToImportGenerator.php

I also setup email confirmation and it seems to work, but the file is not created in uploads directory. The screen shows a confirmation email that I am getting. There is csv file content so I guess the php script is executed properly: enter image description here That is most important part of the code of php script($productsArr are products retrived from database):

function products_to_csv($productsArr, $filename = "productsToImport.csv",     $delimiter=",")
{
$f = fopen('php://memory', 'w');    

$headers = array("Product Name", "Product Url", "excerpt", "description", "Product Status", "Menu_order", "SKU", "Downloadable",
                 "Virtual", "visibility", "quantity", "stock status", "allow backorders", "manage_stock", "regular_price",
                 "sale_price", "weight", "length", "width", "height", "featureD", "sale_price_dates_from",
                 "sale_price_dates_to","download_limit", "download_expiry", "images", "download File name", "type", "category",
                 "product_tag", "shipping class");

fputcsv($f, $headers, $delimiter);

foreach($productsArr as $product)
    fputcsv($f, $product, $delimiter);

fseek($f, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="'.$filename.'";');
fpassthru($f);  
fclose($f); 
}

Should I modify the command somehow to have the csv saved in uploads directory?

Thanks for any help


Solution

  • Your script doesn't save the file on the server, it only sends it as an attachment to the browser opening the script. You should change fpassthru to file_put_contents() to save the file on the server.