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

by 6 replies
7
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
#programming #hdd #php #user #writing
  • For security reasons a PHP script cannot write on the client's computer.

    The best you can do is to give them a download link and let them choose where to save.

    You could use Flash or Java Applet, but even then the access to the client's computer will be limited due to security. But you may be able to save to some directories.

    To be able to save to any directory, you would need them to install a custom browser plug-in or active-x control. And you would need to make MAC and PC versions if you want to support both.
    • [1] reply
    • 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: I could not configure it with the php tags because everytime I did that it deleted the variables.... strange

      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 ?>
      • [2] replies
  • theempero is right. You can't write to user's HDD. Microsoft ActiveX can actually that, but still require explicit permission from users. If it's not a lot of date, cookie will do fine.
  • PHP is a SERVER-SIDE language. It's not a matter of security nor a mater of plugins.

    The PHP Code runs on the server. The client (aka, your PC) only gets the OUTPUT.

    You can't write to the user's HDD. Otherwise, any site you visit could fill your computer with crap.
  • Banned
    [DELETED]

Next Topics on Trending Feed

  • 7

    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.