Error while saving csv file generated by PHP

Posted by user1667374 on Stack Overflow See other posts from Stack Overflow or by user1667374
Published on 2012-09-15T03:28:21Z Indexed on 2012/09/15 3:37 UTC
Read the original article Hit count: 136

Filed under:

I have created below script which is saving the csv file in the same path of this script but when I am saving it to different directory, it gives me error. Can somebody please help me on this?

This is the error:

Warning: fopen(/store/secure/mas/) [function.fopen]: failed to open stream: No such file or directory in /getdetails.php on line 15

Warning: fputs(): supplied argument is not a valid stream resource in /getdetails.php on line 36

Warning: fclose(): supplied argument is not a valid stream resource in /getdetails.php on line 39

This is the script:

<?php

$MYSQL_HOST="localhost";
$MYSQL_USERNAME="***";
$MYSQL_PASSWORD="***";
$MYSQL_DATABASE="shop";
$MYSQL_TABLE="ds_orders";
mysql_connect( "$MYSQL_HOST", "$MYSQL_USERNAME", "$MYSQL_PASSWORD" ) or die( mysql_error( ) );
mysql_select_db( "$MYSQL_DATABASE") or die( mysql_error( $conn ) );


$filename="Order_Details";
$directory = "/store/secure/mas/"; 
$csv_filename = $filename.".csv";

$fd = fopen ( "$directory" . $cvs_filename, "w"); 


$today="2009-03-17";
$disp_date="05122008";
$sql = "SELECT * FROM $MYSQL_TABLE where cDate='$today'";

$result=mysql_query($sql);
$rows=mysql_num_rows($result);
echo $rows;
if(mysql_num_rows($result)>0){

$fileContent="Record ID,Discount Percent\n";
while($data=mysql_fetch_array($result))
{

$fileContent.= "".$data['oID'].",".$data['discount']."\n";
}


$fileContent=str_replace("\n\n","\n",$fileContent);
fputs($fd, $fileContent);
}

fclose($fd);
?>

© Stack Overflow or respective owner

Related posts about php