Jump to content

[SOLVED] download file and delete file from server with one button click


Recommended Posts

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();

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" />

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.

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" />

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.

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>

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.

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>

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>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.