【发布时间】:2013-01-17 08:15:13
【问题描述】:
谁能告诉我我做错了什么?我有一个 php 脚本,它应该使用 cron 作业执行,从数据库中提取数据并将其放入 csv 文件中。 当我从浏览器运行它时,它工作得很好,所以我确定我的查询是正确的。但是,当我使用 cron 作业时,它返回“No input file specified.”并且不写入文件。
这是我的代码:
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
【问题讨论】:
-
您需要在您的fopen路径中添加一个基本路径,目前该路径将从root解析。
-
$fp = fopen("/reports/report.csv" , "w+");检查'/reports/report.csv'
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial.