cURL Issue: prompt user to save file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    cURL Issue: prompt user to save file

    I've have the below code and its works fine, how can I get the file to prompt the user to save it instead of specifying a path on the server to save it?

    I have to use curl due to hosting and memory issues on the hosting.

    Any help much appreciated.
    Code:
    <?php
    $contentType = 'text/plain';
    $file = 'http://mysite.com/test.txt';
    
    $fp = fopen('/path/to/save/mytest.txt', 'w');
    $ch = curl_init($file);
    curl_setopt($ch, CURLOPT_FILE, $fp);       
    $data = curl_exec($ch);       
    curl_close($ch);     
    fclose($fp);
    ?>
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Its not impossible, but not easy,

    What you can do:
    you can map the directories and create a directory tree.

    so that user can browse and choose a place to save.
    and I am positive that this will be your open hole to crack and burn your server.
    ............... ....

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Ok, I'm thinking this option wont be the best so here my dilema, I need a user to be able to click a link to download a file but I need to capture that link click to update the database log table to track what registered users download, any ideas to the best wasy to capture this link click?

      I can't store the files in the database and can't use a script to build the file into a downloadable file due to the size of some of the files 100mb - 200mb+.

      Thanks for the help.

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        in that case never provide the direct link of the file. moreover block the directory from accessing file. Then create a simple download file. everytime you would create a link, you will have to create it with javascript, example below:
        Code:
        <a href='javascript:document.location="download.php?fileName=desire.pdf"'>desire</a>
        download.php
        Code:
        <?php
        $fileName=$_GET['fileName'];
        //do some security check and other operation. before i have posted a script. find it out. it will help a lot
        readfile($fileName);
        ?>

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          Thanks for your reply johny10151981, the reason I was going for capturing the click was due to readfile() being disabled on my hosting for security reasons and the size of some of the files are 200ish MB so I can't really use a function that reads the file into memory first as the server would most definitly fall over.

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            readfile function do not load the file into localmemry, it directly load to output buffer. if it is disable you better create your own function that will behave like readfile function.

            Code:
            <?php
            $handle = fopen($path);
            while (!feof($handle)) {
              echo fread($handle, 1);
            }
            fclose($handle);
            ?>

            Comment

            • johny10151981
              Top Contributor
              • Jan 2010
              • 1059

              #7
              i think this also can be done
              Code:
              <?php
              /*database issues*/
              $fileName=$_GET['fileName'];
              header('Location: www.mysite.com/links/'.$fileName);
              exit;
              ?>

              Comment

              • ziycon
                Contributor
                • Sep 2008
                • 384

                #8
                This code does the job but what affect will/can it have on the memory usage of the server and the limitations of the memory used per script in php.ini?

                For example in php.ini its set to: 'memory_limit = 32M', so I assume this code can't handle a file greater the 32mb!?
                Code:
                $contentType = 'application/pdf';
                $path = 'http://path/to/file/test.pdf';
                
                header("Cache-Control: public");
                header("Content-Description: File Transfer");
                header("Content-Type: $contentType");
                header("Content-Transfer-Encoding: binary");
                header("Content-Disposition: attachment; filename=".basename($path));
                
                $handle = fopen($path,'r');
                while (!feof($handle)) {
                  echo fread($handle, 1);
                }
                fclose($handle);

                Comment

                • johny10151981
                  Top Contributor
                  • Jan 2010
                  • 1059

                  #9
                  Absolutely not.

                  It mean you cannot allocate memory in RAM more than 32 Mega byte.

                  When you open a file, it does not load the file into RAM. If it was it would be impossible to handle big database system.

                  it is possible to limit upload and download file size in your php.ini. in that case you should provide multi-part file download support

                  Comment

                  • ziycon
                    Contributor
                    • Sep 2008
                    • 384

                    #10
                    I have a script in place that will chunck the file being downloaded into 30mb bits to get around the php.ini size restriction, its seems to be working grand for the past few hours.
                    *Fingers crossed*

                    Thanks for the help and advice johny10151981.

                    Comment

                    Working...