Jump to content

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


Chucksta

Recommended Posts

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  :confused:

 

thanks in advance  :D

Link to comment
Share on other sites

 

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

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

 

2) Write files to said folder

// 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

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

 

 

4) Force download of zipped file

 

// 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

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 ?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.