user1078494
user1078494

Reputation: 509

fopen with cron job

Can someone please tell me what I'm doing wrong? I have a php script which is supposed to be executed with a cron job, extract data from a database and put it in a csv file. When I run it from the browser, it works just fine so I'm sure my query is correct. But, when I use cron job, it returns "No input file specified." and doesn't write to the file.

Here's my code:

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));
}
?>

The csv file should be stored under

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

What am I missing here? TIA

Upvotes: 5

Views: 3985

Answers (4)

thaJeztah
thaJeztah

Reputation: 29117

This answer may be off-topic, but MySQL is able to directly output query results to a CSV file using 'INTO OUTFILE'.

note MySQL should be running on the same server as the desired output file location

See here for an example: MySQL export into outfile : CSV escaping chars

Upvotes: 0

colintemple
colintemple

Reputation: 421

The issue is that when you start your fopen() argument with /, you're going to the server root, not the web root. So instead, you either need to give fopen() the full path to the file, or dynamically retrieve the current directory like so:

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

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 882336

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

This won't write to where you're expecting the file to go. It will try to create it in the reports directory at the root of the filesystem.

Upvotes: 1

Arnold Daniels
Arnold Daniels

Reputation: 16573

You're not saving to the correct dir. Instead do

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

Upvotes: 4

Related Questions