Announcement

Collapse
No announcement yet.

Miva output header

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Miva output header

    Hi everyone,

    I had to make a script lately that made me use the miva_output_header function. I wanted to share a small function that can be handy when someone wants to share files ( put files on download ) without making links.

    I wrote an article here: http://mivascript.org/miva_script_fr...ad_script.html . Any comments welcomed...

    PS: I promise i won't write a post for every function I share in the future :D but this one can be tricky...

    Thank you
    Claudiu
    Zen Radio : Relax :) : www.zenradio.fm
    MivaScript Tutorials & Scripts : www.mivascript.org

    #2
    Re: Miva output header

    Hey Claudiu, are you reading my mind? I've been thinking about exactly this subject recently; I need this download capability for a project I'm working on. You just saved me some hours of reading, experimenting, and head-scratching.

    I miss Ivo's contributions around here, but you're a good substitute :) . Thanks --
    Kent Multer
    Magic Metal Productions
    http://TheMagicM.com
    * Web developer/designer
    * E-commerce and Miva
    * Author, The Official Miva Web Scripting Book -- available on-line:
    http://www.amazon.com/exec/obidos/IS...icmetalproducA

    Comment


      #3
      Re: Miva output header

      Hi Kent

      So glad it helps ! That's the big idea with the shared code You have no idea how much your book helped (and still helps) me... so I should thank you first :)

      Thanks
      Claudiu
      Zen Radio : Relax :) : www.zenradio.fm
      MivaScript Tutorials & Scripts : www.mivascript.org

      Comment


        #4
        Creating Excel files with Miva Script

        While trying to find a way to create excel files on the fly, I found a dirty and ugly solution, that you can find here: http://mivascript.org/miva_script_create_excel.html

        It's dirty, as we play with the header, and excel will see that the content of the file is not excel. But I'll try to find a solution for that matter too ...

        Anyway, it's a good solution if you want to give the possibility to export excel file on Linux server, using Miva Script :D

        Any comments welcomed !
        Last edited by Emma; 04-06-09, 08:14 AM.
        Zen Radio : Relax :) : www.zenradio.fm
        MivaScript Tutorials & Scripts : www.mivascript.org

        Comment


          #5
          Re: Miva output header

          I've done something very similar for streaming live .csv and txt reports out as a download.

          As for excel, I've output a .csv file this way.

          <MvASSIGN NAME = "l.null" VALUE = "{ miva_output_header('Content-type', 'application/vnd.ms-excel') }">

          Excel is offered as the default program during the download dialog box, and opens the file just fine.

          A note about .csv files; you need to make sure any data that contains commas gets wrapped in double quotes.
          Ray Yates
          "If I have seen further, it is by standing on the shoulders of giants."
          --- Sir Isaac Newton

          Comment


            #6
            Re: Miva output header

            Hi Claudiu,

            On this page http://mivascript.org/miva_script_fr...ad_script.html

            there is code for force download not for create xls file.

            >>THE CODE

            You can see the code here: http://mivascript.org/share/scripts/...ownload.mv.txt
            Brian.P
            [email protected]

            Comment


              #7
              Re: Miva output header

              Hi Brian

              Yes, indeed, the page is talking about forced download. The excel one is here: http://mivascript.org/miva_script_create_excel.html . I used the same link for the forum apparently :)

              PS: Thanks Ray for your suggestion !

              Best regards,
              Claudiu
              Zen Radio : Relax :) : www.zenradio.fm
              MivaScript Tutorials & Scripts : www.mivascript.org

              Comment


                #8
                Re: Miva output header

                Hi,


                I visited this link http://mivascript.org/miva_script_create_excel.html

                but i only get code for the function force download.did not get function for create xls file.

                Comment


                  #9
                  Re: Miva output header

                  The example is creating the data and outputing the file at the same time, so no physical file is ever created.

                  By setting the headers you get the browser 'file download" dialog box and the contents are streamed to your hard disk instead of to the browser screen.

                  Using Toolbelt, you can even do this in a Storemorph page template. Use Query to get the data, then format it as a simple tab separated text file.

                  I've created custom data export screens for several clients this way.
                  Last edited by RayYates; 05-28-10, 01:19 PM.
                  Ray Yates
                  "If I have seen further, it is by standing on the shoulders of giants."
                  --- Sir Isaac Newton

                  Comment


                    #10
                    Re: Miva output header

                    Hi again,

                    Indeed, this way you cannot save the file, you can only stream it to the browser (and it's a very "amateur" approach with lots of inconvenient). I'll make the function to save the binary excel file this week and I'll post it here. I'll try to do it in mivascript (oh yes, it's possible) or in a C++ library (it's easier).

                    PS: I updated the excel page... You were right, I messed up the links and there was an example of the forced download, not the one with the excel file...

                    Cheers,
                    Claudiu
                    Zen Radio : Relax :) : www.zenradio.fm
                    MivaScript Tutorials & Scripts : www.mivascript.org

                    Comment


                      #11
                      Re: Miva output header

                      For text files, you only have to create a formatted string and use file_create(path, location, outputdata) to save it.

                      Then using the technique shown, you can use file_read(filename, location, inputdata) to read the file start the download, or as shown, you can set up the headers and output the data dynamicaly without even creating a file.

                      The later method is sometimes tricky to get to work. For example I have links within a merchant module, that when clicked, refreshes the module screen so the data can be created. If I start the download, then re-fresh the modules screen, the screens html ends up inside the download data.

                      Instead I save the data to a temporary file in secure location and output javascript at the bottom of the modules screen. When the screen finished re-freshing, the javascript uses window.open() to directly call the modules .mvc file passing parameters. There, I validate the parameters, open the file, set the headers and start the download.

                      Later I delete the temporary file. There are endless variations on this idea, but when possible, streaming the download data as it's generated is a viable option.
                      Ray Yates
                      "If I have seen further, it is by standing on the shoulders of giants."
                      --- Sir Isaac Newton

                      Comment


                        #12
                        Re: Miva output header

                        I"m reopening this thread as I think it really relates to something I want to be able to do. I want my customer to be able to select images and then download them as a zip file from my store. Anyone done that?

                        Comment


                          #13
                          Re: Miva output header

                          Are you planning on creating the zip file dynamically on the server or do they already exist?
                          Ray Yates
                          "If I have seen further, it is by standing on the shoulders of giants."
                          --- Sir Isaac Newton

                          Comment


                            #14
                            Re: Miva output header

                            My thought is to have them created dynamically.
                            Ideally we would do two things...
                            1. we'd have a cron job run that would gather and zip all files together for each category. And we'd provide a link in the category footer for that catagory's images. (I want it to be automated, so I don't have to upload new zip files and do a lot of manual work everyday).
                            Then,
                            2. We'd also have something like a light box, where our customer could go and add selected images to 'hold' them until they got all they wanted.. then we'd create a zip file of all those, that they could download. This way we can delete it after it's created and keep the server clean.
                            It would probably need to track total file size, so they don't make a monster sized file that would time out during download.
                            3. it might be a nice thing during their session, to keep track of what they already downloaded, so they don't get dups and our box doesn't have to do a lot of extra work.
                            4. I think we have way too many images to offer 1 zip file of all of them. So I Was thinking that by category could be a viable alternative.

                            Comment


                              #15
                              Re: Miva output header

                              I've done this before, with different programming languages. From what I can tell, you'd (probably) want to use a different language to create the zip files. But if you wanted to go the native Miva Script route, you could use tar_create to create tar files, but Windows doesn't play nice with them. The other ways would be to write a wrapper library or try to re-create the zip functionality in Miva Script.

                              Here's an example in PHP of how you might go about doing this.

                              Here's the scenario: You have a PHP script which you pass data in through GET/POST data (easier to use GET on the server without using cURL). You can either specify a zip file name, or randomly generate it. You'll pass in the file locations as well though a CSV parameter.

                              Code:
                              <?php
                              /*
                              $_GET['zip_file_name'] = Not required.  Specifies the file name of the Zip file.
                              $_GET['files'] = Required.  CSV list of location of files 
                                      (ex: /httpdocs/full_images/grandpa1.png,/httpdocs/full_images/grandma1.png)
                              */
                              
                              
                              //Create File name
                              if(isset($_GET['zip_file_name'] && $_GET['zip_file_name'] != null) {
                                  $zip_file_name = $_GET['zip_file_name'];
                              } else {
                                  //create a unique file name
                                  $zip_file_name = tempnam("/zip_files/", "tmp");
                              }
                              
                              $zip = new ZipArchive();
                              
                              if($zip->open($zip_file_name, ZIPARCHIVE::CREATE) !== TRUE) {
                                  exit("Error opening <$zip_file_name>\n");
                              }
                              // Separate the files and throw them into an array
                              $files = explode(',', $_GET['files']);
                              
                              // Loop through the array, and add each file individually to the Zip archive
                              foreach($files as $file) {
                                  // adds file to archive, renames it to file name, instead of messing with directory structure
                                  $zip->addFile($file, basename($file));
                              }
                              
                              $zip->close();
                              
                              header("Content-type: application/zip");
                              header("Content-Disposition: attachment; filename=$zip_file_name");
                              
                              ?>
                              That would create them on the fly. You could add in controls too, to check that the file has already been created, and if so, don't write a new one (or write over it, if you've updated the files. This would consistently keep the zip catalog in sync with what you have on the site).

                              This script lends itself toward being able to deliver content which is protected from the public. You can store your information behind the httpdocs folder, and still access it via PHP. So, it provides a somewhat secure way of transmitting sensitive data.
                              PCINET, LLC

                              Miva Merchant Design, Development, Integration & Support
                              We built the most Miva Merchant stores!
                              Miva shopping cart design & integration service and our Portfolio!

                              e-mail: [email protected]
                              web: www.pcinet.com

                              "We who cut mere stones must always be envisioning cathedrals."
                              Quarry Worker's Creed

                              Comment

                              Working...
                              X