I created a script to import a CSV file. When I run it by hand with the php command on the server the script runs without problems. On the other hand when the crontab execute it there is an error:
SPLFILEOBJECT::__CONSTRUCT(IMPORTS/STOCK_EXPRESS_FR.CSV): FAILED TO OPEN STREAM: NO SUCH FILE OR DIRECTORY .
Here is my import script.
<?php
require(dirname(__FILE__) . '/config/config.inc.php');
$dir_fichier = 'IMPORTS/STOCK_EXPRESS_FR.csv';
//error_reporting(E_ALL);
ini_set('display_errors', 0);
tntProduct($dir_fichier);
function tntProduct($dir_fichier)
{
$errors = [];
try{
$csv = new SplFileObject($dir_fichier);
$csv->setFlags(SplFileObject::READ_CSV);
$csv->setCsvControl(';');
} catch (Exception $exception) {
$errors[] = $exception->getMessage();
}
$nbProducts = 0;
$nbProductsSuccess = 0;
foreach ($csv as $ligne) {
if ($ligne[1] === '0') {
$result = getIdsProducts($ligne[0]);
if (!empty($result)) {
foreach ($result as $key => $item) {
$nbProducts++;
try {
setCarrier($item['id_product']);
$nbProductsSuccess++;
}catch (Exception $exception) {
$errors[] = $exception->getMessage();
}
}
}
}
}
if (count($errors)>0) {
sendMail($errors);
} else {
sendMail($nbProducts);
}
if (file_exists ($dir_fichier)) {
unlink($dir_fichier);
}
}
function getIdsProducts($ref)
{
$sql = Db::getInstance()->executeS('
SELECT p.id_product
FROM ' . _DB_PREFIX_ . 'product p
WHERE p.reference = ' . '"' . $ref . '"');
return $sql;
}
function setCarrier($productId)
{
$shop = 1;
$carriersFR = [
0 => 1,
1 => 72,
2 => 87
];
$products = new Product((int)$productId, false, 1, $shop);
$products->setCarriers($carriersFR);
}
function sendMail($result)
{
$data['{message}'] = $result;
Mail::Send(
1,
'tnt_fr',
'Mise à jour TNT France',
$data,
'test@test.fr',
'test',
null,
null,
null,
true,
_PS_MAIL_DIR_,
false,
1
);
}
Do you have an idea of the problem ? Thank you for your help