dmikester1 Posted May 17, 2007 Share Posted May 17, 2007 I want the user to be able to download a CSV file and delete the file from the server all with the click of a button. Currently, in firefox, it will just display the CSV and not delete it. In IE, it will download the CSV and not delete it. This involves both PHP and HTML, but I assumed this was the right place to ask. Thanks Mike Here is my code that I have: /*********************************** Write Date to csv file ***********************************/ $_file = "users.csv"; $_fp = @fopen( $_file, 'a' ); $_csv_data=username.','.First.' '.Name.','.Last.' '.Name.','.Spouse.','.Address.','.City.','.State.','.Zip.','.Home.' '.Phone.','.Cell.' '.Phone.','.Email."\n"; fwrite( $_fp, $_csv_data ); $query = "select * from info"; $result=mysql_query($query); while (list($user, $first, $last, $spouse, $address, $city, $state, $zip, $homePhone, $cellPhone, $email)=mysql_fetch_row($result)) { $_csv_data=$user.','.$first.','.$last.','.$spouse.','.$address.','.$city.','.$zip.','.$homePhone.','.$cellPhone.','.$email."\n"; fwrite( $_fp, $_csv_data ); } fclose( $_fp ); echo "data collected!"; ?> <form name = "getCSV" action="users.csv" method="GET"> <input name="submit" type="submit" value="Download excel file"> <? if (isset($_GET['submit'])) { $do = unlink("users.csv"); } } mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/ Share on other sites More sharing options...
hitman6003 Posted May 17, 2007 Share Posted May 17, 2007 Why are you writing the data to a file to begin with if they are only going to download it and then the file is removed? <?php if ($_POST['submit']) { $query = "select * from info"; $result=mysql_query($query); $csv = username.','.First.' '.Name.','.Last.' '.Name.','.Spouse.','.Address.','.City.','.State.','.Zip.','.Home.' '.Phone.','.Cell.' '.Phone.','.Email."\n"; while ( list($user, $first, $last, $spouse, $address, $city, $state, $zip, $homePhone, $cellPhone, $email) = mysql_fetch_row($result) ) { $csv .= $user.','.$first.','.$last.','.$spouse.','.$address.','.$city.','.$zip.','.$homePhone.','.$cellPhone.','.$email."\n"; } header("Content-length: " . strlen($csv)); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); echo $csv; } ?> <form method="post"><input type="submit" name="submit" value="Get Data" /> Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-255883 Share on other sites More sharing options...
dmikester1 Posted May 17, 2007 Author Share Posted May 17, 2007 The info is being pulled in from my mySQL database. I want one specific user to be able to download the data to view and work with it and then delete it from the server because the data is sensitive and I don't want that file just sitting on the server. Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-255901 Share on other sites More sharing options...
dmikester1 Posted May 21, 2007 Author Share Posted May 21, 2007 Could you explain those header lines to me? Thanks Mike Why are you writing the data to a file to begin with if they are only going to download it and then the file is removed? <?php if ($_POST['submit']) { $query = "select * from info"; $result=mysql_query($query); $csv = username.','.First.' '.Name.','.Last.' '.Name.','.Spouse.','.Address.','.City.','.State.','.Zip.','.Home.' '.Phone.','.Cell.' '.Phone.','.Email."\n"; while ( list($user, $first, $last, $spouse, $address, $city, $state, $zip, $homePhone, $cellPhone, $email) = mysql_fetch_row($result) ) { $csv .= $user.','.$first.','.$last.','.$spouse.','.$address.','.$city.','.$zip.','.$homePhone.','.$cellPhone.','.$email."\n"; } header("Content-length: " . strlen($csv)); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); echo $csv; } ?> <form method="post"><input type="submit" name="submit" value="Get Data" /> Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-258317 Share on other sites More sharing options...
hitman6003 Posted May 21, 2007 Share Posted May 21, 2007 First one tells the browser how much data to expect, so you can get a progress bar during the download. Second one tells it what kind of data (csv). What that does is offer the "open with default application" and it gives you a choice, such as Excel. Without that header, it doesn't know what type of file it is until it's fully stored on the file system and can associate the ".csv" with excel. Third one tells it to download as an attachment and what the filename should be. Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-258336 Share on other sites More sharing options...
dmikester1 Posted May 21, 2007 Author Share Posted May 21, 2007 I had to stick the header lines at the top because it was giving me errors when they were down below. Now it is just giving me a blank csv file. I included all the code in my php file this time. Thank you Mike <?php session_start(); include("database.php"); include("login.php"); header("Content-length: " . strlen($csv)); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Thank you!</title> <link rel="stylesheet" type="text/css" href="mycss.css" /> </head> <body> <br> <br> <?php $user = $_SESSION[username]; if($user != "dan") { $first = ucwords($_POST['first']); $last = ucwords($_POST['last']); $spouse = ucwords($_POST['spouse']); $address = ucwords($_POST['address']); $city = ucwords($_POST['city']); $state = ucwords($_POST['state']); $zip = ucwords($_POST['zip']); $homePhone = ucwords($_POST['homePhone']); $cellPhone = ucwords($_POST['cellPhone']); $email = $_POST['email']; $query = "INSERT INTO info VALUES ('$user','$first','$last','$spouse','$address','$city','$state','$zip','$homePhone','$cellPhone','$email')"; mysql_query($query); } else if($user == "dan") { /*********************************** Write Date to csv file ***********************************/ if ($_POST['submit']) { $query = "select * from info"; $result=mysql_query($query); $csv = username.','.First.' '.Name.','.Last.' '.Name.','.Spouse.','.Address.','.City.','.State.','.Zip.','.Home.' '.Phone.','.Cell.' '.Phone.','.Email."\n"; while ( list($user, $first, $last, $spouse, $address, $city, $state, $zip, $homePhone, $cellPhone, $email) = mysql_fetch_row($result) ) { $csv .= $user.','.$first.','.$last.','.$spouse.','.$address.','.$city.','.$zip.','.$homePhone.','.$cellPhone.','.$email."\n"; } echo $csv; } } mysql_close(); ?> <form method="post"><input type="submit" name="submit" value="Get Data" /> <br /> <br /> <br /> <a href="logout.php">Logout</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-258437 Share on other sites More sharing options...
JakeTheSnake3.0 Posted May 21, 2007 Share Posted May 21, 2007 That's because the $csv variable isn't populated yet...you need to store that $csv variable as a session variable...so at the top of your script, before the header()s, do this.... if (isset($_SESSION['csv'])) { header("Content-length: " . strlen($_SESSION['csv'])); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); unset($_SESSION['csv']); } and change your code below this point so that the $_SESSION['csv'] is set instead of the regular $csv variable. Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-258457 Share on other sites More sharing options...
hitman6003 Posted May 21, 2007 Share Posted May 21, 2007 The session_start() outputs a header to the browser, which I think may be causing a problem. YOu also should not output html to the browser after telling it that you are sending a csv file... Also check to make sure that your included "login.php" isn't outputting to the browser before the csv does. <?php session_start(); include("database.php"); $user = $_SESSION['username']; if($user != "dan") { $first = ucwords($_POST['first']); $last = ucwords($_POST['last']); $spouse = ucwords($_POST['spouse']); $address = ucwords($_POST['address']); $city = ucwords($_POST['city']); $state = ucwords($_POST['state']); $zip = ucwords($_POST['zip']); $homePhone = ucwords($_POST['homePhone']); $cellPhone = ucwords($_POST['cellPhone']); $email = $_POST['email']; $query = "INSERT INTO info VALUES ('$user','$first','$last','$spouse','$address','$city','$state','$zip','$homePhone','$cellPhone','$email')"; mysql_query($query); } else if ($user == "dan") { /*********************************** Write Date to csv file ***********************************/ if ($_POST['submit']) { $query = "select * from info"; $result=mysql_query($query); $csv = username.','.First.' '.Name.','.Last.' '.Name.','.Spouse.','.Address.','.City.','.State.','.Zip.','.Home.' '.Phone.','.Cell.' '.Phone.','.Email."\n"; while ( list($user, $first, $last, $spouse, $address, $city, $state, $zip, $homePhone, $cellPhone, $email) = mysql_fetch_row($result) ) { $csv .= $user.','.$first.','.$last.','.$spouse.','.$address.','.$city.','.$zip.','.$homePhone.','.$cellPhone.','.$email."\n"; } header("Content-length: " . strlen($csv)); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); echo $csv; } } mysql_close(); include("login.php"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Thank you!</title> <link rel="stylesheet" type="text/css" href="mycss.css" /> </head> <body> <br> <br> <form method="post"> <input type="submit" name="submit" value="Get Data" /> </form> <br /> <br /> <br /> <a href="logout.php">Logout</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-258482 Share on other sites More sharing options...
dmikester1 Posted May 22, 2007 Author Share Posted May 22, 2007 I'm just having troubles here. I've tried it with and without the if (isset($_SESSION['csv'])) { header("Content-length: " . strlen($_SESSION['csv'])); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); unset($_SESSION['csv']); } When I click on the button to take me to the Thank you page it actually tries to download the csv file, which ends up being blank still. <?php session_start(); include("database.php"); $user = $_SESSION[username]; if($user != "dan") { $first = ucwords($_POST['first']); $last = ucwords($_POST['last']); $spouse = ucwords($_POST['spouse']); $address = ucwords($_POST['address']); $city = ucwords($_POST['city']); $state = ucwords($_POST['state']); $zip = ucwords($_POST['zip']); $homePhone = ucwords($_POST['homePhone']); $cellPhone = ucwords($_POST['cellPhone']); $email = $_POST['email']; $query = "INSERT INTO info VALUES ('$user','$first','$last','$spouse','$address','$city','$state','$zip','$homePhone','$cellPhone','$email')"; mysql_query($query); } else if($user == "dan") { /*********************************** Write Date to csv file ***********************************/ if ($_POST['submit']) { echo "hey, thanks dude!"; $query = "select * from info"; $result=mysql_query($query); $csv = username.','.First.' '.Name.','.Last.' '.Name.','.Spouse.','.Address.','.City.','.State.','.Zip.','.Home.' '.Phone.','.Cell.' '.Phone.','.Email."\n"; while ( list($user, $first, $last, $spouse, $address, $city, $state, $zip, $homePhone, $cellPhone, $email) = mysql_fetch_row($result) ) { $csv .= $user.','.$first.','.$last.','.$spouse.','.$address.','.$city.','.$zip.','.$homePhone.','.$cellPhone.','.$email."\n"; } header("Content-length: " . strlen($csv)); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); echo $csv; } } mysql_close(); include("login.php"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Thank you!</title> <link rel="stylesheet" type="text/css" href="mycss.css" /> </head> <body> <br /> <br /> <form method="post"> <input type="submit" name="submit" value="Get Data" /> </form <br /> <br /> <br /> <a href="logout.php">Logout</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-258726 Share on other sites More sharing options...
hitman6003 Posted May 22, 2007 Share Posted May 22, 2007 You can not output any data / text to the browser before sending a header... This is incorrect: echo "hey, thanks dude!"; ............... header("Content-length: " . strlen($csv)); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=data.csv"); You must send the headers before anything else. Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-258917 Share on other sites More sharing options...
dmikester1 Posted May 22, 2007 Author Share Posted May 22, 2007 Wow, I never thought that "echo" line could be causing the problem. I actually put that in for debugging to see if it was running that loop or not. Thank you so much. It works!! Mike Quote Link to comment https://forums.phpfreaks.com/topic/51903-solved-download-file-and-delete-file-from-server-with-one-button-click/#findComment-259276 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.