PHP and writing to the the user's HDD, possible ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Chucksta
Forum Newbie
Posts: 5
Joined: Wed Aug 25, 2010 9:06 am

PHP and writing to the the user's HDD, possible ?

Post by Chucksta »

I have a web app hosted on Just Host that I have nearly finished writing but still needs to have coded the ability to write a text file to user's computer. the user should be able to specify where on their computer they would like the file to be stored. Is this possible ? And can you tell me how it's done, or point me to a souce that can tell me :)

I rarely ever ask for help regarding programming, but this has flumoxed me.


If it is not possible to do this, then would I have to generate this file on the Host's server (Just Host in my case), then download it ? If this can be done then can you please tell me how, as any info I have found related to file downloads seems a bit obscure :?:

thanks in advance :D
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP and writing to the the user's HDD, possible ?

Post by pickle »

No, you can't write to the user's hard drive. They'll have to download the file. Look up "force download PHP" & you should get what you need.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Chucksta
Forum Newbie
Posts: 5
Joined: Wed Aug 25, 2010 9:06 am

Re: PHP and writing to the the user's HDD, possible ?

Post by Chucksta »

Thanks for the quick reply. I had a feeling it was not possible. Distinct lack of documentation on the matter was a pretty big clue :)

I'll do as you say, and use force download.

The app will be used by many people, and will actually produce more than one file, so what I'll do is create a folder based on their user name, write the files to that folder. Zip the folder up, then implement that forced download.

Thanks :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP and writing to the the user's HDD, possible ?

Post by pickle »

Is it multiple files per person or just one file?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Chucksta
Forum Newbie
Posts: 5
Joined: Wed Aug 25, 2010 9:06 am

Re: PHP and writing to the the user's HDD, possible ?

Post by Chucksta »

multiple files per person
Chucksta
Forum Newbie
Posts: 5
Joined: Wed Aug 25, 2010 9:06 am

Re: PHP and writing to the the user's HDD, possible ?

Post by Chucksta »

PHP and writing to the the user's HDD, possible ?


Okay, with writing to the user's HDD prohibited, I implemented a force download instead:

In brief:

1) Create folder so hold generated text files

2) Write files to said folder

3) Zip files up

4) Force download of zipped file


In detail:

The above steps had to be done at the end of the PHP code, after the text for the files is generated and placed into an array (contents of array used to create text files on the system).

The code is placed before the ?> tag, which marks the end of the PHP code

1) Create folder to hold generated text files

Code: Select all

mkdir(“filesFolder”);     // I actually use the user's login ID for the folder name
2) Write files to said folder

Code: Select all

// output result 
	$fileName = $folderName . "/file";
	for ($i=0; $i < $totalNumberTextFiles; $i++)
	{
		$ fileName .= $i;    // e.g. fileName becomes “filesFolder/file01”
		file_put_contents($fileName,$textDocuments[$i]);  // store 1 text file
		$ fileName = $folderName . "/file"; // reset file name
	}

3) Zip files up

Code: Select all

// zip folder up
	exec("zip -r text_files.zip " . $folderName . "/");

4) Force download of zipped file

Code: Select all

// force file download
	$file = "text_files.zip";
	// Set headers
	header("Content-Type: application/zip");
	header("Content-Disposition: attachment; filename=$file");
	header("Content-Transfer-Encoding: binary");
	@readfile($file);

5) Tidy up – delete folder

Code: Select all

rmdir($folderName);

et voilà, job done :)

Obviously you cannot say where the file should be downloaded to, but at least it is now going to the user's own computer's HDD.

POSSIBLE ERRORS:
I had a problem with it reporting that the Headers had already been sent.
“Cannot modify header information - headers already sent by.... “

Solved it by removing all the echo statements I had put in during testing.

Basically, when using headers you cannot have echo statements before the Header statements in the PHP code. Also, you cannot have blank spaces before the <?php or after the ?>
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: PHP and writing to the the user's HDD, possible ?

Post by Bind »

Chucksta wrote:POSSIBLE ERRORS:
I had a problem with it reporting that the Headers had already been sent.
“Cannot modify header information - headers already sent by.... “

Solved it by removing all the echo statements I had put in during testing.

Basically, when using headers you cannot have echo statements before the Header statements in the PHP code. Also, you cannot have blank spaces before the <?php or after the ?>
It is always best to attempt to structure your code without using PHP Output Buffer Control, but sometimes it is needed. I am not saying this is needed in your script since you have fixed it, but its good to know the true power of php output buffer control which will indeed allow you to send output prior to headers.
Chucksta
Forum Newbie
Posts: 5
Joined: Wed Aug 25, 2010 9:06 am

Re: PHP and writing to the the user's HDD, possible ?

Post by Chucksta »

Thanks Bind, I'll read up on that PHP Output Buffer.

I'm very new to PHP. I only started coding in it two weeks ago (written 3 apps so far to help me in my Affiliate programming ventures). Prior to that I was a C, C++, and C# programmer.

I am really enjoying PHP, because it is so much like C.
Post Reply