2013-02-01 132 views
1

有人可以告訴我我做錯了什麼嗎?我有一個PHP腳本,應該用cron作業執行,從數據庫中提取數據並將其放入csv文件中。 當我從瀏覽器運行它時,它工作得很好,所以我確定我的查詢是正確的。但是,當我使用cron作業時,它返回「沒有指定輸入文件。」並且不寫入文件。fronn與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.p HP

<?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

+0

你需要一個基本路徑添加到您的fopen路徑,目前路徑將從根目錄解析。 – datasage

+0

$ fp = fopen(「/ reports/report.csv」,「w +」);請檢查'/reports/report.csv' – 2013-02-01 22:28:54

+0

[**請不要在新代碼**中使用'mysql_ *'函數](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

回答

2

您不保存到正確的目錄。相反,做

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

這不會寫信給你意料中的文件中去。它會嘗試在文件系統根目錄下的reports目錄中創建它。

0

問題是,當你用/開始你的fopen()參數時,你將轉到服務器根目錄,而不是web根目錄。因此,相反,你要麼需要給fopen()的完整路徑的文件或動態檢索當前目錄,如下所示:

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

這個答案可能是題外話,但MySQL是能夠直接輸出查詢結果一個使用'INTO OUTFILE'的CSV文件。

的MySQL應該在同一臺服務器所需的輸出文件的位置上運行

在這裏看到一個例子: MySQL export into outfile : CSV escaping chars