我创建了一个脚本来生成Excel工作表,然后将其附加到邮件中。当我使用浏览器手动运行它时它工作正常。当我将其设置为cron时,它开始给我一个错误。
PHP警告:
fopen(uploads/2016_07_29_18_36_42.xls): failed to open stream: No such file or directory in /var/www/html/docomo-live/application/libraries/Classes/PHPExcel/Shared/OLE/PPS/Root.php on line 90
PHP Fatal error: Uncaught exception 'Exception' with message 'Can't open uploads/2016_07_29_18_36_42.xls. It may be in use or protected.' in /var/www/html/docomo-live/application/libraries/Classes/PHPExcel/Shared/OLE/PPS/Root.php:93
Stack trace:
#0 /var/www/html/docomo-live/application/libraries/Classes/PHPExcel/Writer/Excel5.php(233): PHPExcel_Shared_OLE_PPS_Root->save('uploads/2016_07...')
#1 /var/www/html/docomo-live/Cron_to_positive_cases.php(100): PHPExcel_Writer_Excel5->save('uploads/2016_07...')
#2 /var/www/html/docomo-live/Cron_to_positive_cases.php(167): getPositiveData(Array)
#3 {main}
thrown in /var/www/html/docomo-live/application/libraries/Classes/PHPExcel/Shared/OLE/PPS/Root.php on line 93
我已经尝试了很多但却无法解决它
下面是我的代码
<?php
/******************** Database details ***********************************/
$dbHost ='****************';
$userName = '***************';
$userPassword = '*********';
$dbName = '*************';
/*************************************************************************/
set_time_limit(0);
date_default_timezone_set('Asia/Kolkata');
$conn= mysql_connect($dbHost,$userName,$userPassword);
mysql_select_db($dbName,$conn);
if($conn)
{
echo "connection successful";
}
else
{
echo "connection unsuccessful";
}
include('application/libraries/Classes/PHPExcel.php');
function getPositiveData($id)
{
$headings = array('DATE','TIME','CAF NO','CUSTOMER NAME','DEL NO','DEALER NAME','CLUSTER','DEALER CODE');
$fileName=NULL;
$fileName= date("Y_m_d_H_i_s").".xls";
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('AV Positive Cases');
$objPHPExcel->getActiveSheet()->setAutoFilter('A1:H1');
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
$objPHPExcel->getActiveSheet()
->getStyle('C')
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_NUMBER
);
$rowNumber = 1;
$col = 'A';
/*********************** Add Headings into the excel *****************************/
foreach($headings as $heading)
{
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
$i = 2;
foreach($id as $key=>$value)
{
if($value['wl_id']!="")
{
$sql=mysql_query("select caf_no,concat(cust_first_name,' ',cust_last_name) as cust_name,'' as del_number,dealer_description as dealer_name,location_id as cluster,dealer_code from wl_task_details where wl_id='".$value['wl_id']."'");
}
elseif($value['fresh_id']!="")
{
$sql=mysql_query("select caf_no,cust_name,del_number,dealer_name,cluster,dealer_code from fresh_task_details where fresh_id='".$value['fresh_id']."'");
}
/************************* Add Data into excel ***********************************/
while($row=mysql_fetch_assoc($sql))
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, date('Y-m-d',strtotime($value['pos_timestamp'])))
->setCellValue('B'.$i, date('h:i:s',strtotime($value['pos_timestamp'])))
->setCellValue('C'.$i, $row['caf_no'])
->setCellValue('D'.$i, $row['cust_name'])
->setCellValue('E'.$i, $row['del_number'])
->setCellValue('F'.$i, $row['dealer_name'])
->setCellValue('G'.$i, $row['cluster'])
->setCellValue('H'.$i, $row['dealer_code']);
}
$i++;
}
$objPHPExcel->getActiveSheet()->freezePane('A2');
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$path= "uploads/".$fileName;
//chmod($path,0755);
$objWriter->save($path);
$subject = "FTD Positive Cases ";
$msgBody= "Hi,";
$msgBody = $msgBody.'<br/><br/>Regards,<br/>Verification Team';
$msgBody = $msgBody.'<br/><br/>Please login into your SmartAV account by using the following link ';
$msgBody = $msgBody.'<br/><br/><a href="www.universalmanagement.co.in">www.universalmanagement.co.in</a>';
$from = '*********';
$to = '*************';
$headers .= "From: $from \r\n"."Reply-To: $to \r\n";
$headers .= 'Cc: ***********';
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($path))); // Set your file path here
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/html; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $msgBody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; name=\"{$fileName}\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
if($mail)
{
echo "<br />Mail sent";
$query = mysql_query("insert into cron_status (date_time,status) values (now(),'1')");
}
else
{
echo "<br /> Mail not sent";
$query = mysql_query("insert into cron_status (date_time,status) values (now(),'0')");
}
}
/**************************** Function Ends *****************************************************/
/******************* Query To get Positive cases ********************************/
$query=mysql_query("select task_id,wl_id,fresh_id,pos_timestamp from task_alloc where pos_timestamp like '%".date('Y-m-d')."%'") or die(mysql_error());
$task_id=array();
while($row=mysql_fetch_assoc($query))
{
array_push($task_id,$row);
}
getPositiveData($task_id);
/********************************************************************************/
?>