php-general Digest 9 Jul 2012 17:19:03 -0000 Issue 7881

Topics (messages 318411 through 318413):

Using named Pipes between PHP and ZIP
        318411 by: Dennis Heck
        318412 by: tamouse mailing lists
        318413 by: Dennis Heck

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Hello everyone,

I'm looking for a solution for the following:
1) collecting data from a simple html form and send them to a php script
2) take the data and place them in a php-generated excel sheet
3) zip the excel sheet and password protect the zip file (standard encription will be sufficent, no AES needed)
4) send the zip file as an email attachment
This kind of output is fixed, changing the output is not an option.

I'm ok with building the excel sheet and sending the mail, but I'm still trying and error with the zip. Since I need password protection in the zip, I cannot use the php build in zip extension. I guess I will need to call ZIP on the shell. I'm aware I could first save the generated excel sheet to the filesystem and afterwards call the zip process on the file, putting the zip file also to the file system. But I wonder if there's an option to stream the data till they finally can be put to the multipart mail - without saving anything to the filesystem.

Here is my first try, just a little modification of the example on proc_open in the php docu. To keep the test simple, I load a sample xls from the filesystem instead of generating from the php class and output it directly to the browser. The script works well except one point: the file contained in the zip will have no name an no file extension.

   <?php
   header ('Content-type: application/zip');
   header ('Content-Disposition: attachment; filename="download.zip"');

   $descriptorspec = array(
       0 => array("pipe", "r"),
      1 => array("pipe", "w"),
      2 => array("file", "error-output.txt", "a")
   );

   $dateiname = 'xyz.xls';
   $file = fread(fopen($dateiname, "r"), filesize($dateiname));

   $process = proc_open('zip -P 1234', $descriptorspec, $pipes);

   if (is_resource($process)) {

       fwrite($pipes[0], $file);
       fclose($pipes[0]);

       $zip = stream_get_contents($pipes[1]);
       fclose($pipes[1]);

       $return_value = proc_close($process);
   }

   echo $zip;
   ?>

So I look for an option to get the name and extension in the zipfile. Here I found named pipes as an option. Calling via shell, tested with a textfile in order to use less

   mkfifo xyz.txt
   less readme.txt > xyz.txt & zip output.zip -FI xyz.txt -P 1234
   rm xyz.txt

Now I get a pw protected zip file containing xyz.txt. Now how can I combine both? How can I tell php to use the named pipe instead of stdin in proc_open? Or any other idea to get the file in the zip the correct name and extension.

Thanks in advance,
Regards,
Dennis



--- End Message ---
--- Begin Message ---
≈On Sun, Jul 8, 2012 at 11:30 AM, Dennis Heck <n...@dennisheck.de> wrote:
> Hello everyone,
>
> I'm looking for a solution for the following:
> 1) collecting data from a simple html form and send them to a php script
> 2) take the data and place them in a php-generated excel sheet
> 3) zip the excel sheet and password protect the zip file (standard
> encription will be sufficent, no AES needed)
> 4) send the zip file as an email attachment
> This kind of output is fixed, changing the output is not an option.
>
> I'm ok with building the excel sheet and sending the mail, but I'm still
> trying and error with the zip. Since I need password protection in the zip,
> I cannot use the php build in zip extension. I guess I will need to call ZIP
> on the shell. I'm aware I could first save the generated excel sheet to the
> filesystem and afterwards call the zip process on the file, putting the zip
> file also to the file system. But I wonder if there's an option to stream
> the data till they finally can be put to the multipart mail - without saving
> anything to the filesystem.
>
> Here is my first try, just a little modification of the example on proc_open
> in the php docu. To keep the test simple, I load a sample xls from the
> filesystem instead of generating from the php class and output it directly
> to the browser. The script works well except one point: the file contained
> in the zip will have no name an no file extension.
>
>    <?php
>    header ('Content-type: application/zip');
>    header ('Content-Disposition: attachment; filename="download.zip"');
>
>    $descriptorspec = array(
>        0 => array("pipe", "r"),
>       1 => array("pipe", "w"),
>       2 => array("file", "error-output.txt", "a")
>    );
>
>    $dateiname = 'xyz.xls';
>    $file = fread(fopen($dateiname, "r"), filesize($dateiname));
>
>    $process = proc_open('zip -P 1234', $descriptorspec, $pipes);
>
>    if (is_resource($process)) {
>
>        fwrite($pipes[0], $file);
>        fclose($pipes[0]);
>
>        $zip = stream_get_contents($pipes[1]);
>        fclose($pipes[1]);
>
>        $return_value = proc_close($process);
>    }
>
>    echo $zip;
>    ?>
>
> So I look for an option to get the name and extension in the zipfile. Here I
> found named pipes as an option. Calling via shell, tested with a textfile in
> order to use less
>
>    mkfifo xyz.txt
>    less readme.txt > xyz.txt & zip output.zip -FI xyz.txt -P 1234
>    rm xyz.txt
>
> Now I get a pw protected zip file containing xyz.txt. Now how can I combine
> both? How can I tell php to use the named pipe instead of stdin in
> proc_open? Or any other idea to get the file in the zip the correct name and
> extension.
>
> Thanks in advance,
> Regards,
> Dennis
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

I *think* all you need in the first paramter to proc_open is :

    zip -P password - -

The first - is the zip file -- which will be send to stdout.
The second - is the input -- which you should pipe your spreadsheet into.

Good luck!

--- End Message ---
--- Begin Message ---

Unfortunately it makes no difference if i use zip with 2 - or if I leave them ommited. The longer I guess about it, the more I think it might be a ZIP topic, namely how the stream to stdin needs to be like so zip will know the name of the file from it.

Regards,
Dennis
--- End Message ---

Reply via email to