{"id":1773,"date":"2013-04-04T06:30:37","date_gmt":"2013-04-04T05:30:37","guid":{"rendered":"http:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1773"},"modified":"2013-05-26T14:07:17","modified_gmt":"2013-05-26T13:07:17","slug":"php-upload-di-piu-file-o-immagini-contemporaneamente","status":"publish","type":"post","link":"https:\/\/www.manuelmarangoni.it\/sir-bit\/1773\/php-upload-di-piu-file-o-immagini-contemporaneamente\/","title":{"rendered":"PHP &#8211; Upload di pi\u00f9 file o immagini contemporaneamente"},"content":{"rendered":"<div id=\"attachment_1776\" style=\"width: 425px\" class=\"wp-caption aligncenter\"><a class=\"lightbox\" title=\"L\u2019invio e il recupero di pi\u00f9 file da un form upload \u00e8 piuttosto semplice. Basta seguire qualche accortezza e lavorare sugli array\" href=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2013\/04\/Php-e-ingranaggi.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1776\" class=\"size-medium wp-image-1776 \" style=\"margin-bottom: 5px;\" title=\"Php e ingranaggi\" alt=\"Php - multi upload di file\" src=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2013\/04\/Php-e-ingranaggi-415x300.jpg\" width=\"415\" height=\"300\" srcset=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2013\/04\/Php-e-ingranaggi-415x300.jpg 415w, https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2013\/04\/Php-e-ingranaggi.jpg 487w\" sizes=\"(max-width: 415px) 100vw, 415px\" \/><\/a><p id=\"caption-attachment-1776\" class=\"wp-caption-text\">L\u2019invio e il recupero di pi\u00f9 file da un form upload \u00e8 piuttosto semplice. Basta seguire qualche accortezza e lavorare sugli array (1)<\/p><\/div>\n<p>Qualche giorno fa mi \u00e8 stato chiesto per email di realizzare un articolo sugli upload multipli, cio\u00e8 uno script che sia in grado di fare l\u2019upload di pi\u00f9 file o immagini contemporaneamente. La richiesta \u00e8 avvenuta dopo la lettura di un mio recente articolo che trattava le basi dell\u2019<a title=\"PHP \u2013 Form e controlli per l\u2019upload di immagini da parte dell\u2019utente\" href=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/1608\/php-form-e-controlli-per-lupload-di-immagini-da-parte-dellutente\/\">upload di immagini<\/a>.<\/p>\n<p>Esistono in rete <strong>diversi sistemi per eseguire l\u2019upload multiplo<\/strong>. I pi\u00f9 efficienti si avvalgono di una struttura a classi e permettono opzioni di livello avanzato.<br \/>\nPer lo scopo di questo articolo, per\u00f2, ci fermeremo ai primi passi: invio dei file e recupero di ognuno di essi.<\/p>\n<p>Si richiede la lettura (che \u00e8 molto semplice) dell\u2019articolo linkato sopra, sulla quale fonder\u00f2 le basi di questo tutorial.<\/p>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">Il form<\/h3>\n<p>La creazione del form \u00e8 molto semplice. L\u2019unica cosa da ricordarsi \u00e8 che l\u2019attributo \u201cname\u201d deve contenere un nome con le parentesi quadre e <strong>che sia presente l\u2019attributo \u201cmultiple\u201d<\/strong>.<br \/>\nDi fatto, quando cercherete i file sul computer, potrete selezionarne pi\u00f9 di uno tenendo premuto il tasto ctrl della tastiera.<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"html4strict\" style=\"font-family: monospace;\"><span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">form<\/span> <span style=\"color: #000066;\">action<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"\"<\/span> <span style=\"color: #000066;\">method<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"post\"<\/span> <span style=\"color: #000066;\">enctype<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"multipart\/form-data\"<\/span>&gt;<\/span>\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">input<\/span> <span style=\"color: #000066;\">type<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"file\"<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"file[]\"<\/span> multiple&gt;<\/span>\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">input<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"invia\"<\/span> <span style=\"color: #000066;\">type<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"submit\"<\/span> <span style=\"color: #000066;\">value<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"Carica i file\"<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">form<\/span>&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><!--more--><\/p>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">Il recupero delle informazioni<\/h3>\n<p>Diamo un&#8217;occhiata al <strong>recupero delle informazioni<\/strong>. In seguito all&#8217;invio, i file saranno passati tramite l&#8217;array &#8220;$_FILES[&#8216;file&#8217;][&#8216;name&#8217;]&#8221;.<br \/>\nNell&#8217;esempio i file saranno salvati nella cartella &#8220;upload_img&#8221;, la cui stringa \u00e8 immagazzinata nella variabile $folder.<\/p>\n<p><script type=\"text\/javascript\">\/\/ <![CDATA[\ngoogle_ad_client = \"ca-pub-9984963620559913\"; \/* Sir Bit - post singolo rettangolare *\/ google_ad_slot = \"5334366715\"; google_ad_width = 336; google_ad_height = 280;\n\/\/ ]]><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\/\/ <![CDATA[\n\n\n\/\/ ]]><\/script><\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #000088;\">$folder<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #0000ff;\">'upload_img\/'<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #990000;\">isset<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$_FILES<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">'file'<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">'name'<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\r\n    <span style=\"color: #000088;\">$count<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">;<\/span>\r\n    <span style=\"color: #b1b100;\">foreach<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$_FILES<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">'file'<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">'name'<\/span><span style=\"color: #009900;\">]<\/span> <span style=\"color: #b1b100;\">as<\/span> <span style=\"color: #000088;\">$filename<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n        <span style=\"color: #000088;\">$destination<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #000088;\">$folder<\/span><span style=\"color: #339933;\">;<\/span>\r\n        <span style=\"color: #000088;\">$origin<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #000088;\">$_FILES<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">'file'<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #0000ff;\">'tmp_name'<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #000088;\">$count<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">;<\/span>\r\n        <span style=\"color: #000088;\">$count<\/span><span style=\"color: #339933;\">++;<\/span>\r\n        <span style=\"color: #000088;\">$destination<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #000088;\">$destination<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #990000;\">basename<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$filename<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n        <span style=\"color: #990000;\">move_uploaded_file<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$origin<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$destination<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n    <span style=\"color: #009900;\">}<\/span>\r\n\r\n<span style=\"color: #009900;\">}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Da notare la variabile $count, un intero che si incrementer\u00e0 a ogni ciclo. Sar\u00e0 questa variabile a stabilire di volta in volta quale file prendere tra quelli passati.<\/p>\n<div style=\"margin-top: 100px; margin-bottom: 20px;\"><strong><em>Copyright immagini<\/em><\/strong><\/div>\n<p>(1) http:\/\/www.talentsfromindia.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>L\u2019invio e il recupero di pi\u00f9 file da un form upload \u00e8 piuttosto semplice. Basta seguire qualche accortezza e lavorare sugli array. Ecco un esempio di base.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[4],"class_list":["post-1773","post","type-post","status-publish","format-standard","hentry","category-php-mysql","tag-php","cat-78-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1773"}],"collection":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/comments?post=1773"}],"version-history":[{"count":6,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1773\/revisions"}],"predecessor-version":[{"id":1890,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1773\/revisions\/1890"}],"wp:attachment":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/media?parent=1773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/categories?post=1773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/tags?post=1773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}