A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearsend a file to client - page 1
User InfoPosts
send a file to client#1
I want to write a text file in the server through Php, and have the client to download that file.

How would i do that?

Essentially the client should be able to download the file from the server.

posted date: 2009-04-10 00:23:00

Re: send a file to client#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-10 00:23:01

Re: send a file to client#3
PHP has a number of very simplistic, C-like functions for writing to files. Here is an easy example:<?php// first parameter is the filename//second parameter is the modifier: r=read, w=write, a=append$handle = fopen("logs/thisFile.txt", "w");$myContent = "This is my awesome string!";// actually write the file contentsfwrite($handle, $myContent);// close the file pointerfclose($handle);?>It(s a very basic example, but you can find more references to this sort of operation here:PHP fopen

posted date: 2009-04-10 00:28:00

Re: send a file to client#4
Just post a link on the site to http://www.mydomain.com/textfile.phpAnd in that PHP file you put the following code:<?phpheader((Content-Type: text/plain();print "The output text";?>That way you can create the content dynamic (from a database)...Try to Google to oter "Content-Type" if this one is not the one you are looking for.

posted date: 2009-04-10 00:31:00

Re: send a file to client#5
In addition to the data already posted, there is a header you might want to try. Its only a suggestion to how its meant to be handled, and the user agent can chose to ignore it, and simply display the file in the window if it knows how: <?php header((Content-Type: text/plain(); # its a text file header((Content-Disposition: attachment(); # hit to trigger external mechanisms instead of inbuiltSee Rfc2183 for more on the Content-Disposition header.

posted date: 2009-04-10 00:37:00

Re: send a file to client#6
If you set the content type to application/octet-stream, the browser will ALWAYS offer file as a download, and will never attempt to display it internally, no matter what type of file it is. <?php filename="download.txt"; header("Content-type: application/octet-stream"); header("Content-disposition: attachment;filename=$filename"); // output file content here?>

posted date: 2009-04-10 00:43:00

Re: send a file to client#7
I didn't know about "Content-Disposition." Thanks for the info. +1

posted date: 2009-04-10 00:45:00

Re: send a file to client#8
PS: although it might work with the little d on some UA's, its not the RFC spec so you're best to use the upper-case version.

posted date: 2009-04-10 00:56:00

Re: send a file to client#9
the other problem with relying on octet-stream, is you force the user to save it, because it cant use MIME to determine what application to open it with externally, and that's a problem.

posted date: 2009-04-10 01:00:00

Re: send a file to client#10
This is the best way to do it, supposing you don(t want the user to see the real URL of the file.<?php $filename="download.txt"; header("Content-disposition: attachment;filename=$filename"); readfile($filename);?>Additionally, you could protect your files with mod_access.

posted date: 2009-04-10 01:21:00

Re: send a file to client#11
@Kent Fredric: According to RFC, the header names are case-insensitive.

posted date: 2009-04-10 09:54:00

Re: send a file to client#12
@Kent Fredric: Forcing the user to save instead of open an external app was kind of the point.

posted date: 2009-04-10 09:55:00

select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.