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

  • Home
  • SEARCH
  • 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 7957443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:15:01+00:00 2026-06-04T04:15:01+00:00

I want the users of my php script to be able to download the

  • 0

I want the users of my php script to be able to download the contents of a certain text file from my server but I don’t want them to download it directly from my server so I want to go through a php file called download.php

I use this code to trigger the file prompt

header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=textfile.txt");
header("Content-Length: " . filesize('textfile.txt');

$fp = fopen('textfile.txt', "r");
fpassthru($fp);
fclose($fp);

The code works, the file gets downloaded but it seems the code ignores new lines of that text file.

Let’s say the content of the original text file is

word1
word2
word3
word4
word5

The content of the downloaded file would be

word1word2word3word4word5

How can I fix it that the downloaded file actually retains the new lines of the original text file?

  • 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-06-04T04:15:02+00:00Added an answer on June 4, 2026 at 4:15 am

    Here’s a code to convert text file EOLs from Linux, Windows and Mac to Windows.
    So no matter what EOLs your file has it will open fine on Win.

    header("Content-Type: text/plain");
    header("Content-Disposition: attachment; filename=textfile.txt");
    header("Content-Length: " . filesize('textfile.txt'));
    
    $f = file_get_contents('textfile.txt');
    $f = str_replace("\r\n", "\n", $f); //Convert Windows to Unix
    $f = str_replace("\r", "\n", $f); //Convert Mac to Unix
    $f = str_replace("\n", "\r\n", $f); //Convert Unix to Windows
    echo $f;
    

    The code is short but it’s not too good for huge files.

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

Sidebar

Related Questions

I want users to be able to upload files, but I don't want them
I want to make a little php poll. The script should ask the users
I want to allow a user to download an html file using a php
I want to be able to post on users Twitter feed from a Facebook
There is this PHP script on my website which I don't want people to
I already wrote a PHP script that allows users to upload images but I
I want to allow registered users of a website ( PHP ) to upload
I want to create a site in PHP with cakephp that users can insert
I'm working in PHP to allow users to delete content and I want delete
I like CKEditor and my server uses PHP. I want to use CKEditor to

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.