texmansru47 Posted October 27, 2008 Share Posted October 27, 2008 All, I have been struggling to create a very simple CSV file (a physical file) from php. I have tried several variations, but here is what I have currently: <?php ini_set('display_errors', 1); error_reporting(E_ALL); $newline = "<br />"; $space =" "; if (isset($_GET['param1']) && !empty($_GET['param1'])){ $BatchNumData=$_GET['param1']; echo "The Batch Number is:\n$BatchNumData and the Shipment is listed as Good.".$newline.$newline; } elseif ($_SERVER['REQUEST_METHOD'] == 'POST'){ $BatchNumData=$_POST['param1']; echo "The Batch Number is:\n$BatchNumData".$newline.$newline; // Connect to mysql database $con = mysql_connect("localhost","user","XXXXXX") or die('Connection: ' . mysql_error());; mysql_select_db("datatest", $con) or die('Database: ' . mysql_error()); //************************************************************ //** Create CSV file for Labeling MP ** //************************************************************ $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; $mpit=$_POST['MPID']; $sql = "SELECT * FROM `ShipTemp` WHERE `BatchNum` = '$BatchNumData' AND `MPBoxNum` = '$mpit'"; $results = mysql_query($sql); $sernumi=$results['serNum']; $model=$results['OEMSku']; $masterid=$results['MPBoxNum']; $_file = 'optlbl1.csv'; $_fp = @fopen( $_file, 'w' ); while (list($sernum,$model,$masterid)=mysql_fetch_row($results)) { $_csv_data=$imei.','.$model.','.$masterid. "\n"; @fwrite( $_fp, $_csv_data ); } @fclose( $_fp ); mysql_close($con); } ?> and I get NOTHING for output. I know I'm missing something, but what? There are NO GOOD tutorials out there and please do not suggest php.net, since I have been all over that and nothing there is clear enough to figure out the root cause of why my codes are not working. Any ideas? Link to comment https://forums.phpfreaks.com/topic/130308-csv-creation-headaches/ Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 remove the @ signs from the front of the file handling functions and see if it reports any errors Link to comment https://forums.phpfreaks.com/topic/130308-csv-creation-headaches/#findComment-675842 Share on other sites More sharing options...
texmansru47 Posted October 27, 2008 Author Share Posted October 27, 2008 You mean the following: $_fp = @fopen( $_file, 'w' ); // should be $_fp = fopen( $_file, 'w' ); @fwrite( $_fp, $_csv_data ); // should be fwrite( $_fp, $_csv_data ); @fclose( $_fp ); // should be fclose( $_fp ); Would that be correct? Link to comment https://forums.phpfreaks.com/topic/130308-csv-creation-headaches/#findComment-675861 Share on other sites More sharing options...
texmansru47 Posted October 27, 2008 Author Share Posted October 27, 2008 if so, I still do not get a file prompt to save a file. I get nothing. Link to comment https://forums.phpfreaks.com/topic/130308-csv-creation-headaches/#findComment-675867 Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 oh...you want it to prompt for download? the script you posted will save it to a file where the script is. try this for having it prompt for download: replace this: $_file = 'optlbl1.csv'; $_fp = @fopen( $_file, 'w' ); while (list($sernum,$model,$masterid)=mysql_fetch_row($results)) { $_csv_data=$imei.','.$model.','.$masterid. "\n"; @fwrite( $_fp, $_csv_data ); } @fclose( $_fp ); with $_file = 'optlbl1.csv'; $_contents = ""; while (list($sernum,$model,$masterid)=mysql_fetch_row($results)) { $_contents .= $imei.','.$model.','.$masterid. "\n"; } header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" ); header ( "Pragma: no-cache" ); header ( "Content-type: text/x-csv" ); header ( "Content-Disposition: attachment; filename={$_file}" ); header ( "Content-Length: ". strlen($contents) ); echo $contents; exit; Link to comment https://forums.phpfreaks.com/topic/130308-csv-creation-headaches/#findComment-675882 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.