dongmaqiu6084 2018-07-05 07:15
浏览 79
已采纳

错误导入CSV文件Crontab

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

  • 写回答

1条回答 默认 最新

  • douya7282 2018-07-05 07:22
    关注

    In crontab run the script, is not be in the folder, so you need to write full path or correct relative path.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致