Ciao, avete bisogno di effettuare un'upload multiplo con php?? Bene, il seguente codice potrebbe fare al caso vostro
prima di tutto vi consiglio di leggere questo: Upload con PHP
Il tutto si compone di 3 file:
config.php <-- il file di configurazione
upload.class.php <-- il file della classe per eseguire l'upload
esterno.js <-- file javascript con il codice per mostrare i campi file multipli (http://www.xaraya.com/index.php/xarbb/topic/269)
Per fare i primi test create un file index.php con il seguente codice:
il codice potrà essere aggiornato/modificato aggiungendo nuove funzioni, così come la parte HTML del form (uso di fogli di stile, etc.).codice:<?php error_reporting(E_ALL); require_once("config.php"); require_once("upload.class.php"); // ISTANZIA LA CLASSE $upload=new upload(); // SE L'ARRAY $_FILES CONTIENE QUALCOSA ESEGUI IL CODICE PER CARICARE I FILE if (count($_FILES) > 0) $upload->caricafile(); // ALTRIMENTI MOSTRA IL FORM PER L'UPLOAD else $upload->mostraform(); ?>
---------------------------------------------------------
file config.php
---------------------------------------------------------codice:<?php $dir_upload=""; // DIRECTORY DOVE EFFETTUARE L'UPLOAD $chmod_dir=0700; // PERMESSI DELLA DIRECTORY $debug=0; // 1=STAMPA ALCUNE SEMPLICI INFORMAZIONI DI DEBUG, 0=NON STAMPARE $sovrascrivi_file=0; // 1=SOVRASCRIVI I FILES SE ESISTENTI, 0=NON SOVRASCRIVERE I FILES $dim_massima=500; // DIMENSIONE MASSIMA UPLOAD IN KB $check_tipi=1; //1=CONTROLLA IL TIPO DI FILE, 0=NON CONTROLLARE $tipi_permessi=array( //ARRAY PER ALCUNI TIPI DI FILE "text/plain", "text/html", "text/xml", "image/jpeg", "image/gif", "image/png", "video/mpeg", "audio/midi", "application/x-zip-compressed", "application/vnd.ms-excel", "application/x-msdos-program", "application/octet-stream" ); ?>
file upload.class.php
---------------------------------------------------------codice:<?php // file upload.class.php // // Marco Barcaro // 27/12/2004 // Testato con Apache 1.3.29, PHP 4.3.10, su winXP // Testato con Apache 1.3.26, PHP 4.1.2, su Debian $dir_upload=(substr($dir_upload,-1) != "/" && $dir_upload != "") ? $dir_upload."/" : $dir_upload; define("DIR_UPLOAD",$dir_upload); define("CHMOD_DIR",$chmod_dir); define("DEBUG",$debug); define("SOVRASCRIVI_FILE",$sovrascrivi_file); define("DIM_MASSIMA",$dim_massima*1024); define("CHECK_TIPI",$check_tipi); define("TIPI_PERMESSI",implode("|",$tipi_permessi)); class upload { function upload(){ } function caricafile(){ //CODICE PER L'UPLOAD CON VARI CONTROLLI if (count($_FILES) > 0){ $numero_file= count($_FILES['file']['tmp_name']); for($i=0;$i<$numero_file;$i++){ if($_FILES['file']['size'][$i] == 0){ echo "L'UPLOAD DEL FILE {$_FILES['file']['name'][$i]} NON E' ANDATO A BUON FINE! \n"; unset( $_FILES['file']['name'][$i]); unset( $_FILES['file']['type'][$i]); unset( $_FILES['file']['size'][$i]); unset( $_FILES['file']['error'][$i]); unset( $_FILES['file']['tmp_name'][$i]); } } $numero_file=count( $_FILES['file']['tmp_name']); echo "Hai caricato $numero_file file(s)"; echo " \n"; foreach($_FILES['file']['name'] as $chiave=>$valore){ if (DEBUG == 1){ echo "Nome file: ".$_FILES['file']['name'][$chiave]." \n"; echo "Tipo file: ".$_FILES['file']['type'][$chiave]." \n"; echo "Dimensione: ".$_FILES['file']['size'][$chiave]." byte \n"; echo "Nome temporaneo: ".$_FILES['file']['tmp_name'][$chiave]." \n"; } if (is_uploaded_file( $_FILES['file']['tmp_name'][$chiave])){ if ($_FILES['file']['size'][$chiave] <= DIM_MASSIMA){ if(CHECK_TIPI == 0 || (CHECK_TIPI == 1 && in_array( $_FILES['file']['type'][$chiave], explode("|",TIPI_PERMESSI)))){ if(!is_dir(DIR_UPLOAD) && DIR_UPLOAD != ""){ if( !@mkdir(DIR_UPLOAD,CHMOD_DIR)) die("ERRORE NELLA CREAZIONE DELLA DIRECTORY ".DIR_UPLOAD.""); } if(!file_exists(DIR_UPLOAD.$_FILES['file']['name'][$chiave]) || SOVRASCRIVI_FILE == 1){ if (@move_uploaded_file( $_FILES['file']['tmp_name'][$chiave], DIR_UPLOAD.$_FILES['file']['name'][$chiave])) echo "FILE {$_FILES['file']['name'][$chiave]} TRASFERITO!"; else die("ERRORE NEL TRASFERIMENTO DEL FILE ".$_FILES['file']['name'][$chiave].""); } else echo ("IL FILE ".$_FILES['file']['name'][$chiave]." E' ESISTENTE!"); } else echo ("IL TIPO DI FILE ".$_FILES['file']['type'][$chiave]." NON E' CONSENTITO!"); } else echo ("LA DIMENSIONE DEL FILE ".$_FILES['file']['type'][$chiave]." NON E' CONSENTITA!"); } else die("ERRORE NEL TRASFERIMENTO DEL FILE ".$_FILES['file']['name'][$chiave].""); echo "<hr />\n"; } } } function mostraform(){ //FORM PER EFFETTUARE L'UPLOAD echo "<html> <head> <script type=\"text/javascript\" src=\"esterno.js\"></script> </head> <body> <form action=\"{$_SERVER['PHP_SELF']}\" method=\"POST\" name=\"modulo\" enctype=\"multipart/form-data\"> <div id=\"attachment\" style=\"display:none\"> <input id=\"file\" name=\"file\" type=\"file\" size=\"55\" /> <a href=\"#\" onclick=\"javascript:removeFile(this.parentNode.parentNode,this.parentNode);\"> Rimuovi</a> </div> <div id=\"attachments\"> <a id=\"addupload\" href=\"javascript:addUpload('file')\">Aggiungi file</a> <input name=\"file[]\" type=\"file\" size=\"55\" /> <span id=\"attachmentmarker\"></span> </div> <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"".DIM_MASSIMA."\" /> <input type=\"submit\" value=\"invia\" /> </form> </body> </html>\n"; } } ?>
file esterno.js
codice:var max = 0; // maximum # of attachments allowed var currentUploads = 0; // current # of attachment sections on the web page var nameDesc = ''; // Name property for the Description Input field var nameFile = ''; // Name property for the File Input field var scrollPosVert = 0; // stores the current scroll position on the form // for some reason when a div is taken out, the form // will scroll to the top on both Firefox and IE // SCROLL FUNCTIONS function saveScrollPos(offset){ scrollPosVert=(document.all)?document.body.scrollTop:window.pageYOffset-offset; } function setScrollPos(){ window.scrollTo(0, scrollPosVert); setTimeout('window.scrollTo(0, scrollPosVert)',1); } // This function adds a new attachment section to the form // It is called when the user clicks the "Attach a file" button... // It takes three arguments: // maxUploads - the maximum number of attachments allowed // descFieldName - the field name for the Description Input field // fileFieldName - the field name for the File Input field function addUpload(fileFieldName){ nameFile=fileFieldName; currentUploads++; if (currentUploads>0) document.getElementById('addupload').childNodes[0].data='Aggiungi file'; // First, clone the hidden attachment section var newFields = document.getElementById('attachment').cloneNode(true); newFields.id = ''; // Make the new attachments section visible newFields.style.display = 'block'; // loop through tags in the new Attachment section // and set ID and NAME properties // // NOTE: the control names for the Description Input // field and the file input field are created // by appending the currentUploads variable // value to the nameFile and nameDesc values // respectively // // In terms of Xaraya, this means you'll need to name your // DD properties will need names like the following: // "AttachmentDesc1" // "AttachmentFile1" // "AttachmentDesc2" // "AttachmentFile2" // "AttachmentDesc3" // "AttachmentFile3" // et cetera... var newField = newFields.childNodes; for (var i=0;i<newField.length;i++){ if (newField[i].name==nameFile){ newField[i].id=nameFile+currentUploads; newField[i].name=nameFile+'[]'; //newField[i].name=nameFile+currentUploads; } } // Insert our new Attachment section into the Attachments Div // on the form... var insertHere = document.getElementById('attachmentmarker'); insertHere.parentNode.insertBefore(newFields,insertHere); } // This function removes an attachment from the form // and updates the ID and Name properties of all other // Attachment sections function removeFile(container, item){ // get the ID number of the upload section to remove var tmp = item.getElementsByTagName('input')[0]; var basefieldname = ''; basefieldname = nameFile; var iRemove=Number(tmp.id.substring(basefieldname.length, tmp.id.length)); // Shift all INPUT field IDs and NAMEs down by one (for fields with a // higher ID than the one being removed) var x = document.getElementById('attachments').getElementsByTagName('input'); for (i=0;i<x.length;i++){ basefieldname=nameFile; var iEdit = Number(x[i].id.substring(basefieldname.length, x[i].id.length)); if (iEdit>iRemove){ x[i].id=basefieldname+(iEdit-1); x[i].name=basefieldname+(iEdit-1); } } // Run through all the DropCap divs (the number to the right of the attachment // section) and update that number... x=document.getElementById('attachments').getElementsByTagName('div'); for (i=0;i<x.length;i++){ // Verify this is actually the "dropcap" div if (x[i].id.substring(0, String('dropcap').length)=='dropcap'){ ID = Number(x[i].id.substring(String('dropcap').length, x[i].id.length)); // check to see if current attachment had a higher ID than the one we're // removing (and thus needs to have its ID dropped) if (ID>iRemove){ x[i].id='dropcap'+(ID-1); x[i].childNodes[0].data=(ID-1); } } } currentUploads--; saveScrollPos(0); container.removeChild(item); setScrollPos(); document.getElementById('addupload').style.visibility='visible'; if (currentUploads==0) document.getElementById('addupload').childNodes[0].data='Aggiungi file'; }



Rispondi quotando

