与cron工作联系

时间:2013-02-01 22:26:32

标签: php cron

有人可以告诉我我做错了什么吗?我有一个php脚本,应该用cron作业执行,从数据库中提取数据并将其放在csv文件中。 当我从浏览器运行它时,它工作正常,所以我确定我的查询是正确的。但是,当我使用cron作业时,它会返回“未指定输入文件。”,并且不会写入该文件。

这是我的代码:

CRON:

  

/home/ACCOUNT_NAME/public_html/directory/report.sh

report.sh

php -q /home/ACCOUNT_NAME/public_html/directory/report.php
php -q /home/ACCOUNT_NAME/public_html/directory/report_mail.php

report.php

<?php

    $con = mysql_connect("localhost", "my_un", "my_pw") ;

    if(!$con){
        die('Could not connect: ' . mysql_error()) ;
        }
    mysql_select_db("my_db", $con) ;

if (!function_exists('fputcsv')){
        function fputcsv($objFile,$arrData,$strDelimiter=',',$strEnclose='"') {
            $strNewLine="\n";
            $strToWrite = '';
            foreach ($arrData as $strCell){
                //Test if numeric
                if (!is_numeric($strCell)){
                    //Escape the enclose
                    $strCell = str_replace($strEnclose,$strEnclose.$strEnclose,$strCell);
                    //Not numeric enclose
                    $strCell = $strEnclose . $strCell . $strEnclose;
                }
                if ($strToWrite==''){
                    $strToWrite = $strCell;
                } else {
                    $strToWrite.=  $strDelimiter . $strCell;
                }
            }
            $strToWrite.=$strNewLine;
            fwrite($objFile,$strToWrite);
        }
}

// CUT - MY QUERY HERE


$fp = fopen("/reports/report.csv" , "w+");

foreach ($list as $line) {
    fputcsv($fp, split(',', $line));
}
?>

csv文件应存储在

  

/home/ACCOUNT_NAME/public_html/directory/reports/report.csv

我在这里缺少什么? TIA

4 个答案:

答案 0 :(得分:2)

你没有保存到正确的目录。而是做

$fp = fopen(__DIR__ . "/reports/report.csv" , "w+");

答案 1 :(得分:1)

问题在于,当您使用fopen()启动/参数时,您将转到服务器根目录,而不是Web根目录。因此,您需要为fopen()提供文件的完整路径,或者动态检索当前目录,如下所示:

$this_directory = dirname( __FILE__ );
$fp = fopen($this_directory . "/reports/report.csv" , "w+");

答案 2 :(得分:0)

$fp = fopen("/reports/report.csv" , "w+");

这不会写入您期望文件的位置。它将尝试在文件系统根目录的reports目录中创建它。

答案 3 :(得分:0)

这个答案可能是偏离主题的,但是MySQL能够使用“INTO OUTFILE”直接将查询结果输出到CSV文件。

note MySQL应与所需的输出文件位置在同一服务器上运行

点击此处查看示例: MySQL export into outfile : CSV escaping chars

相关问题