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

6 replies
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
#hdd #php #user #writing
  • Profile picture of the author theemperor
    Originally Posted by Chucksta View Post

    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
    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.
    Signature
    Learn to code faster, and remove the roadblocks. Get stuff done and shipped! PM me and I can help you with programming tutoring, specialising in Web and the following languages: Javascript ~ HTML ~ CSS ~ React ~ JQuery ~ Typescript ~ NodeJS ~ C#.
    {{ DiscussionBoard.errors[2515249].message }}
    • Profile picture of the author Chucksta
      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 ?>
      {{ DiscussionBoard.errors[2518844].message }}
      • Profile picture of the author techgirl
        You can use cookies if you want to write on clients computer but i dont think there is another way to do it
        {{ DiscussionBoard.errors[2518964].message }}
      • Profile picture of the author adrianwinston
        Originally Posted by Chucksta View Post

        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
        = . "/file";
        for (=0; < ; ++)
        {
        $ fileName .= ; // e.g. fileName becomes "filesFolder/file01"
        file_put_contents(,); // store 1 text file
        $ fileName = . "/file"; // reset file name
        }


        3) Zip files up
        =========
        // zip folder up
        exec("zip -r text_files.zip " . . "/");


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


        5) Tidy up - delete folder
        ================
        rmdir();


        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 ?>
        Agreed. Good to have. Bookmarked.!
        {{ DiscussionBoard.errors[2519052].message }}
  • Profile picture of the author dealbert.net
    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.
    {{ DiscussionBoard.errors[2579522].message }}
  • Profile picture of the author RuiGomes
    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.
    {{ DiscussionBoard.errors[2579737].message }}

Trending Topics