Chucksta Posted August 25, 2010 Share Posted August 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/211700-php-and-writing-to-the-the-users-hdd-possible/ Share on other sites More sharing options...
Adam Posted August 25, 2010 Share Posted August 25, 2010 PHP has no access to the user's file system to be able to write to it as such, however you can force the download of a file on your server so that they're able to choose where to save it? Read this post for how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/211700-php-and-writing-to-the-the-users-hdd-possible/#findComment-1103575 Share on other sites More sharing options...
Chucksta Posted August 25, 2010 Author Share Posted August 25, 2010 Thanks for the quick reply. I had a feeling it could not write directly to the user's HDD. I'll take a look and dissect that post/code you have pointed to. Once I resolve it, I'll post what I did here Quote Link to comment https://forums.phpfreaks.com/topic/211700-php-and-writing-to-the-the-users-hdd-possible/#findComment-1103581 Share on other sites More sharing options...
Chucksta Posted August 26, 2010 Author Share Posted August 26, 2010 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/211700-php-and-writing-to-the-the-users-hdd-possible/#findComment-1103873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.