📜 ⬆️ ⬇️

Browser loading of several files

If you need to give the user the ability to download multiple files, the traditional solution at the moment is to use Flash for this purpose (more rarely, Java applet or ActiveX). In case the corresponding plugin is not available, the user is usually shown a standard HTML element for downloading the file.

The latter situation can be improved by using the built-in browsers ability to download multiple files. Of all browsers, now this feature does not support only Internet Explorer (however, we have not yet seen version 9, something may change there), other browsers are Opera 9 and higher (as well as versions 3.5–6.05), Firefox 3.6+, Chrome 3.0.191.0+ and Safari 4.0.1+ provide this feature.

It is enough to write something like
Copy Source | Copy HTML < form enctype ="multipart/form-data" method ="post"> < input type ="file" min ="1" max ="9999" name ="file[]" multiple ="true" /> < input type ="submit" name ="submit" /> </ form >
  1. Copy Source | Copy HTML < form enctype ="multipart/form-data" method ="post"> < input type ="file" min ="1" max ="9999" name ="file[]" multiple ="true" /> < input type ="submit" name ="submit" /> </ form >
  2. Copy Source | Copy HTML < form enctype ="multipart/form-data" method ="post"> < input type ="file" min ="1" max ="9999" name ="file[]" multiple ="true" /> < input type ="submit" name ="submit" /> </ form >
  3. Copy Source | Copy HTML < form enctype ="multipart/form-data" method ="post"> < input type ="file" min ="1" max ="9999" name ="file[]" multiple ="true" /> < input type ="submit" name ="submit" /> </ form >
  4. Copy Source | Copy HTML < form enctype ="multipart/form-data" method ="post"> < input type ="file" min ="1" max ="9999" name ="file[]" multiple ="true" /> < input type ="submit" name ="submit" /> </ form >
PHP turned out to be ready for such a construction (square brackets are in the “name” parameter for it), it will simply decompose the downloaded files into the elements of the $ _FILES array, unless we use “Opera”.
')
Unfortunately, “Opera” (since version 3.5) sends, when using multiboot, files in the “multipart / mixed” container, which PHP does not understand.

I tried to correct this situation.

Fortunately for us, PHP, accepting such a request, will put its contents into the $ _POST array (in this case, it will go to $ _POST ['file'] [0], then all we have to do is parse it and put it in $ _FILES (I hope the directive your magic_quotes_gpc is disabled).

As a parser, I used the mailparse PECL module (there is a binary for Windows ).

In my example, the parameter “file” is expected, but this value is easy to put into configuration. The code seems simple enough to me not to comment on it, but if something is not clear, ask, I will add comments.
Copy Source | Copy HTML
  1. if ( isset ( $ _POST [ 'file' ], $ _POST [ 'file' ] [ 0 ])) {
  2. if ( $ idx = strpos ( $ _POST [ 'file' ] [ 0 ], "\ n" )) {
  3. $ bound = substr ( $ _POST [ 'file' ] [ 0 ], 2 , $ idx - 2 );
  4. $ body = "MIME-Version: 1.0 \ nContent-type: multipart / form-data; boundary = {$ bound} \ n \ n" .
  5. $ _POST [ 'file' ] [ 0 ];
  6. unset ( $ _POST [ 'file' ] [ 0 ]);
  7. $ f = & $ _FILES [ 'file' ];
  8. $ f [ 'name' ] = $ f [ 'type' ] = $ f [ 'tmp_name' ] = $ f [ 'error' ] = $ f [ 'size' ];
  9. $ msg = mailparse_msg_create ();
  10. if (mailparse_msg_parse ( $ msg , $ body )) {
  11. $ i = 0 ;
  12. foreach (mailparse_msg_get_structure ( $ msg ) as $ st ) {
  13. $ section = mailparse_msg_get_part ( $ msg , $ st );
  14. $ data = mailparse_msg_get_part_data ( $ section );
  15. if ( $ data [ 'content-type' ] == 'multipart / form-data' ) {
  16. continue ;
  17. }
  18. ob_start ();
  19. if (mailparse_msg_extract_part ( $ section , $ body )) {
  20. $ tmp = tempnam (sys_get_temp_dir (), 'php' );
  21. file_put_contents ( $ tmp , ob_get_clean ());
  22. $ f [ 'name' ] [ $ i ] = $ data [ 'disposition-filename' ];
  23. $ f [ 'type' ] [ $ i ] = $ data [ 'content-type' ];
  24. $ f [ 'tmp_name' ] [ $ i ] = $ tmp ;
  25. $ f [ 'error' ] [ $ i ] = 0 ;
  26. $ f [ 'size' ] [ $ i ] = filesize ( $ tmp );
  27. $ i ++;
  28. } else {
  29. ob_end_clean ();
  30. }
  31. }
  32. }
  33. unset ( $ f );
  34. mailparse_msg_free ( $ msg );
  35. }
  36. }
I'm not quite sure about the publication of this article in the blog "PHP", perhaps the "HTML" would be more suitable, on the other hand, here is considered a way to use multiple downloads together in PHP.

PS transferred to "Web Development", as suggested in the comments, really the blog is much closer to the topic.

Source: https://habr.com/ru/post/76749/


All Articles