Jump to content

Raise a "File Download" Dialog Box for text file


ramanjaneyulu

Recommended Posts

Hi How To Raise a "File Download" Dialog Box for text file in php page.I have the following link in php page.<a href="downloadfile.txt" >downloadfile</a>if I click this link it's opening in the browser itself.but our requirement is, it should open the file download dialog box.Thanking you,

Link to comment
Share on other sites

You will need to create a PHP page to send specific headers such as these:header( "Content-Type: application/octet-stream" ); header( "Content-Length:" . filesize( $path ) ); header( "Content-Disposition: attachment; filename=$filename" );Where $path and $filename contain the path to the file and the name you want the download box to show. Once you send those headers, use file_get_contents to send the contents of the file itself.

Link to comment
Share on other sites

I have the links to the text file like this.<a href="File1.txt" >DownloadFile1</a><br /><a href="File2.txt" >DownloadFile2</a><br /><a href="File3.txt" >DownloadFile3</a><br /><a href="File4.txt" >DownloadFile4</a><br /><a href="File5.txt" >DownloadFile5</a><br />So if I click any of the link, it has to open the file download box.So my questions. According to your suggestion we need to read each file.Is there is any other way to over come this problem.

Link to comment
Share on other sites

<a href="download_file.php?file=File1%2Etxt" >DownloadFile1</a><br /><a href="download_file.php?file=File2%2Etxt" >DownloadFile2</a><br /><a href="download_file.php?file=File3%2Etxt" >DownloadFile3</a><br /><a href="download_file.php?file=File4%2Etxt" >DownloadFile4</a><br /><a href="download_file.php?file=File5%2Etxt" >DownloadFile5</a><br />The download_file.php script needs to read the filename from the querystring and do what I described above.

Link to comment
Share on other sites

Hi Thank you so much, I used the following code according your suggestions its working fine.I have one question. Is it any problem if the file size is big? (10MB)$FileName = $_GET["DwnFileName"];header( "Content-Type: application/octet-stream" ); header( "Content-Disposition: attachment; filename=$FileName" );$Path = "FielPath";$Filename_Path = $Path.$FileName;$FielContents = file_get_contents($Filename_Path);print "$FielContents";

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...