savagenoob Posted March 31, 2009 Share Posted March 31, 2009 I wrote some code to export data from the website to a specific program, the extension of the files it uses is .dat. When I do the export on my local machine it works beautifully, but off of the hosted site it doesnt work. I am thinking its a header issue and maybe someone can help. Here is a chunk of code. <?php $date=date("m,d,Y"); $ID=$_GET['ID']; $result = mysql_query("SELECT * FROM clients WHERE ID = $ID"); while($myrow = mysql_fetch_assoc($result)) { header("Content-Type: application/force-download"); header( "Content-Type:text/octet-stream" ); header('Content-disposition: attachment; filename="client.dat"'); ?> "0","0","0","0028836" "1","37","1","<?php echo $myrow['First_Name'];?> <?php echo $myrow['Middle_Name'];?> <?php echo $myrow['Last_Name'];?>","<?php echo $myrow['Phys_Street'];?>","<?php echo $myrow['Phys_City'];?>","<?php echo $myrow['Phys_Zip'];?>","34","","35","<?php echo $myrow['Phone'];?>","1","2","CA - FSC RATER","<?php echo $date;?>","1641","32","<?php echo $myrow['Employer'];?>","<?php echo $myrow['Emp_Street'];?> <?php echo $myrow['Emp_City'];?> <?php echo $myrow['Emp_State'];?> <?php echo $myrow['Emp_Zip'];?>","","","<?php echo $myrow['Mail_Street'];?>","<?php echo $myrow['Mail_City'];?>","<?php echo $myrow['Mail_Zip'];?>","2","<?php echo $myrow['Agent'];?>","TV","0","1","1","0","0","0.00","0.00","0","0","0","<?php echo $date;?>","P","","207","0","0","0","<?php echo $myrow['First_Name'];?>","<?php echo $myrow['Last_Name'];?>","Topa","1168.00","<?php echo $date;?>","A","0","<?php echo $myrow['Emp_Street'];?>","<?php echo $myrow['Emp_City'];?>","<?php echo $myrow['Emp_State'];?>","<?php echo $myrow['Emp_Zip'];?>","0.00","CA","207","1129.00","39.00","0.00","<?php echo $date;?> <?php } ?> I know that line looks like jibberish, its just a hack job on a client file and I am using database data to fill it in... Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/ Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 In what way does it not work? Do you not get data or does the application find the file invalid or something? Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798099 Share on other sites More sharing options...
savagenoob Posted March 31, 2009 Author Share Posted March 31, 2009 it opens the program but the file does not open and an error message says "error reading file" so im assuming its corrupt. when i save instead of open it saves without errors but does not open either and says the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798114 Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 comment out the headers and make sure that the content is what you expect... Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798122 Share on other sites More sharing options...
savagenoob Posted March 31, 2009 Author Share Posted March 31, 2009 Nice tip on commenting out the headers... but everything looks fine and it still wont open. What sucks is that it is working perfectly locally... Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798151 Share on other sites More sharing options...
premiso Posted March 31, 2009 Share Posted March 31, 2009 Try this out. It uses the HEREDOC syntax to help with less confusion. I also removed all the extra going in and out of php and echos. This way you only have to echo once, and one echo is quicker than 20. <?php $date=date("m,d,Y"); $ID=$_GET['ID']; $result = mysql_query("SELECT * FROM clients WHERE ID = $ID"); while($myrow = mysql_fetch_assoc($result)) { $output = '"0","0","0","0028836"\n'; $output .= <<<INFO "1","37","1","{$myrow['First_Name']} {$myrow['Middle_Name']} {$myrow['Last_Name']}","{$myrow['Phys_Street']}", "{$myrow['Phys_City']}","{$myrow['Phys_Zip']}","34","","35","{$myrow['Phone']}\",\"1\",\"2\",\"CA - FSC RATER\" ,"{$date}","1641","32","{$myrow['Employer']}","{$myrow['Emp_Street']} {$myrow['Emp_City']} {$myrow['Emp_State']} {$myrow['Emp_Zip']}" ,"","","{$myrow['Mail_Street']}","{$myrow['Mail_City']}","{$myrow['Mail_Zip']}","2","{$myrow['Agent']}" ,"TV","0","1","1","0","0","0.00","0.00","0","0","0","{$date}" . '","P","","207","0","0","0" ,"{$myrow['First_Name']}","{$myrow['Last_Name']}","Topa","1168.00","{$date}","A","0","{$myrow['Emp_Street']}","{$myrow['Emp_City']}" ,"{$myrow['Emp_State']}","{$myrow['Emp_Zip']}","0.00","CA","207","1129.00","39.00","0.00","{$date}" INFO; } // unsure why this was in the loop... header("Content-Type: application/force-download"); header( "Content-Type:text/octet-stream" ); header('Content-disposition: attachment; filename="client.dat"'); echo $output; ?> Also moved the header items outside of the loop, I do not know why you put them in the loop in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798173 Share on other sites More sharing options...
savagenoob Posted March 31, 2009 Author Share Posted March 31, 2009 I copied this just as you have and get the same result. The data is corrupting along the way somehow. Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798191 Share on other sites More sharing options...
savagenoob Posted April 1, 2009 Author Share Posted April 1, 2009 The fact that this is working perfectly locally and not uploaded to hosting tells me that maybe the header info is wrong? Are those the correct headers for this type of thing? Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798364 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 You are setting content-type twice. I doubt that is kosher. Check out the header manual and try removing the application/force header and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798627 Share on other sites More sharing options...
savagenoob Posted April 1, 2009 Author Share Posted April 1, 2009 OK, I think I made progress at least, I decided to try to pass the file size in the header and it seemed to work but the program said an unexpected parameter was passed, which I can work on and fix... I will update. Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798850 Share on other sites More sharing options...
savagenoob Posted April 1, 2009 Author Share Posted April 1, 2009 I think the problem is that top line... when I take out the headers the \n shows on the page, I tried using <?php $content = '"0","0","0","0028836"'; $content .= "\n"; ?> It doesnt show but it doesnt move it to a new line either. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/151953-export-to-program-help/#findComment-798865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.