Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:40:47+00:00 2026-05-23T14:40:47+00:00

I generate a *.csv file with PHP and it works great. The problem is,

  • 0

I generate a *.csv file with PHP and it works great. The problem is, it contains a bunch of sensitive data. I’ve done my permissions very carefully, so you can’t get to the download link or generate the file without being the correct user.
The issue that I run into, is that right after the user downloads the file, the file needs to be deleted right away. The chance is small, I realize, that someone would be sitting there scanning my site structure, but the data is important enough to where I want to cover my ass 🙂

I had this at first:

<a href="<?php echo base_path().$ourFileName ?>"><?=$ourFileName?></a>

Which works great to just download the file, but I was having a hard time doing any kind of actions with it, because if you nest PHP code inside an OnClick=”” it will be executed before the click happens, because all PHP code is rendered server-side before the HTML and JAVASCRIPT is processed…right? I searched the internet, and it didn’t seem like I could get javascript to have the perms to delete the file…admittedly, I could be very wrong.

So I turned my link into a form:

<?PHP
if(isset($_POST['submit']))
{
    header("Location: ".base_path().$ourFileName);
    unlink(base_path().$ourFileName);
}?>

<form action="<? $PHP_SELF ?>" method="post">
<input type="submit" value="Submit" name="submit">
</form>

But obviously, the unlink() will never be reachable because of the header…at least I think…
Is there another way besides header() to download the file to them?
I realize I could just run a cron job every minute or something and just:

rm -rf *.csv

And that would take care of the issue, but what happens if a user is in the process of downloading the file and cron starts to run? Or the page is generated, but they don’t click on the link for a while for some reason?

Maybe there’s some way to configure my .htaccess file so you can only access the file if linking from a page?

Am I over complicating this?

Any ideas gurus of the interweb?

–edit
Per popular suggestion, I’m working on the new header format so the code looks like this:

<?PHP if(isset($_POST['submit']) && ($user->uid))
{
    myroom_render_csv($ourFileName, $csv_string);
}?>

<form action="<? $PHP_SELF ?>" method="post">
<input type="submit" value="Submit" name="submit">
</form>

and in a separate file:

<?PHP function myroom_render_csv($file_name, $csv_string) {
header('Content-type: text/csv');
$header_string2="Content-Disposition: attachment; filename=\"".$file_name."\"";
header($header_string2);
echo $csv_string;
} ?>

but I get a dump of the full HTML page AND the CSV data. Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:40:48+00:00Added an answer on May 23, 2026 at 2:40 pm

    Here’s a simpler solution – don’t create a file. Instead of writing your data to a .csv file, just set the headers in a php script like so:

    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename="whatever.csv"');
    
    echo $csv_data;
    

    Print out your csv data in the php script, and the browser will treat it like a file download. This way, you’re free to control to who and when the file is accessible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to generate data in Excel. I generated .CSV file. So
I am generating a simple csv file using php. The file contains some user's
Hi I'm using the following code to generate a CSV file containing data from
I used Python to generate a CSV file. But when I open it in
I generate csv files with fputcsv and it works just fine, but when I
I generate csv files with fputcsv and it works just fine, but when I
I have an xml schema and csv data to generate corresponding xml files. I
I am trying to generate a CSV file from DB Query as source one
I've created a custom solution in WordPress that will generate a CSV file to
I wonder if there is any way to generate culture neutral CSV file or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.