0

我有以下在 cron 中运行的代码,

<?php
    $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
    $directory = $_SERVER["DOCUMENT_ROOT"]."/cron/";
    $file = "register_warranty_". date("dMY") .".csv";
    $filepath = $directory.$file;

    if(!file_exists($filepath)) 
    {
        $fp = fopen($filepath, "w+");
        fwrite($fp, "Title, Initials, Surname, HomeTel, Mobile, HouseNumber, Address, Address2, Town, Postcode, NewlyBuiltHome, InstallationDate, Houseowner, InstallersName, InstallersEmail, InstallersMobile, InstallersGasSafeNumber, BusinessName, BusinessAddress, BusinessAddress2, BusinessTown, BusinessRegion, BusinessPostcode, BusinessGasSafeNumber, BusinessEmail, SuppliersName, Model, serial, MaintenanceContract, ExpiryDate, Donotwishtobecontactedviapost, Donotwishtobecontactedviatelephone, Donotwishtobecontactedviaemail, Donotwishtobecontactedviasms\n");
        fclose($fp);
    }

?>

在运行 cron 时收到的 Cron 电子邮件中 ​​- 我收到以下错误,

第 1 行:?php:没有这样的文件或目录
第 3 行:意外标记附近的语法错误$_SERVER['DOCUMENT_ROOT'] = dirname( FILE );'('
line 3:

我是不是做错了什么,我对 Cron 工作不是最有经验的。

4

2 回答 2

1

你可以做两件事之一:

  1. 从 as 执行您的 PHP 脚本crontab

    /usr/bin/php -q script.php

  2. 在 PHP 脚本的开头添加shebang行:

    #!/usr/bin/php -q

    并为您的脚本授予执行权限:

    chmod +x script.php

于 2012-10-31T10:23:00.837 回答
0

我想你忘了在 cronjob 中的文件名之前添加 php 命令。

php myscript.php

于 2012-10-31T10:16:43.023 回答