upload multiplo

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
43
Massa, Italy
Salve, perché questo codice che avevo scritto mesi fa.. solo che faceva vedere le informazioni nell'array..
ed ora vorrei fare che mi caricasse i file nella cartella upload/.
ecco il codice:
Mi potete aiutarmi a sistemare il codice?

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Multi File Upload</title>
</head>
<body>
<?php
if(isset($_POST['btnSubmit'])){
$cartella = 'upload/';

$percorso = $_FILES['dynamic']['tmp_name'];
$nome = $_FILES['dynamic']['name'];
$tipo_file = $_FILES['dynamic']['type'];

if ($tipo_file == "image/jpeg" || $tipo_file == "image/pjpeg" || $tipo_file == "image/gif" || $tipo_file == "image/png") {

echo "file non permesso";

} else {

    foreach($_FILES['dynamic'] as $chi_1 => $val_1){
        echo "$chi_1:<br>";
            foreach($val_1 as $chi_2 => $val_2){
			
                if (copy($percorso, $cartella.$nome)) {

				echo "".$val_1."<br>";

				echo "Upload eseguito con successo";
				} else {

				echo "Si sono verificati dei problemi durante l'Upload";

				}
            }
			}
		}
	}	
?>
<form id="form1"  enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p id="upload-area">
   <input name="dynamic[1]" id="dynamic" type="file"  size="60" />
</p>

<input name="AddFile" id="AddFile" type="button" value="Add file" onclick="addFileUploadBox()" />
<p><input name="btnSubmit" id="btnSubmit" type="submit" value="upload"></p>
<span id="Span1"  />

<script type="text/javascript">
function addFileUploadBox()
{
    if (!document.getElementById || !document.createElement)
        return false;
        
    var uploadArea = document.getElementById ("upload-area");
    
    if (!uploadArea)
        return;

    var newLine = document.createElement ("br");
    uploadArea.appendChild (newLine);
    
    var newUploadBox = document.createElement ("input");
    
    // Set up the new input for file uploads
    newUploadBox.type = "file";
    newUploadBox.size = "60";
    
    // The new box needs a name and an ID
    if (!addFileUploadBox.lastAssignedId)
        addFileUploadBox.lastAssignedId = 100;
        
    newUploadBox.setAttribute ("id", "dynamic" + addFileUploadBox.lastAssignedId);
    newUploadBox.setAttribute ("name", "dynamic[" + addFileUploadBox.lastAssignedId+"]");
    uploadArea.appendChild (newUploadBox);
    addFileUploadBox.lastAssignedId++;
}
</script>
</form>
</body>
</html>

grazie mille e buona giornata.
 
c'è lo fatta solo che non so fare bene il controllo dei file..perché il controllo dei file non funziona.
mi potete aiutarmi ad fare il controllo dei file?
ecco il codice:
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Multi File Upload</title>
</head>
<body>
<?php
if(isset($_POST['btnSubmit'])){
$cartella = 'upload/';
$tipo_file = $_FILES['dynamic']['type'];

                foreach($_FILES['dynamic']['tmp_name'] as $key=>$value)
				{
						move_uploaded_file($value, dirname(__FILE__)."/upload/".$_FILES['dynamic']['name'][$key]);
				}
		}
?>
<form id="form1"  enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p id="upload-area">
   <input name="dynamic[]" id="dynamic" type="file"  size="60" />
</p>

<input name="AddFile" id="AddFile" type="button" value="Add file" onclick="addFileUploadBox()" />
<p><input name="btnSubmit" id="btnSubmit" type="submit" value="upload"></p>
<span id="Span1"  />

<script type="text/javascript">
function addFileUploadBox()
{
    if (!document.getElementById || !document.createElement)
        return false;
        
    var uploadArea = document.getElementById ("upload-area");
    
    if (!uploadArea)
        return;

    var newLine = document.createElement ("br");
    uploadArea.appendChild (newLine);
    
    var newUploadBox = document.createElement ("input");
    
    // Set up the new input for file uploads
    newUploadBox.type = "file";
    newUploadBox.size = "60";
    
    // The new box needs a name and an ID
    if (!addFileUploadBox.lastAssignedId)
        addFileUploadBox.lastAssignedId = 100;
        
    newUploadBox.setAttribute ("id", "dynamic" + addFileUploadBox.lastAssignedId);
    newUploadBox.setAttribute ("name", "dynamic[" + addFileUploadBox.lastAssignedId+"]");
    uploadArea.appendChild (newUploadBox);
    addFileUploadBox.lastAssignedId++;
}
</script>
</form>
</body>
</html>

vi ringrazio molto.
 
Ultima modifica:
salve, ho fatto cosi, ma non funziona ancora o meglio se carico immagini jpg e altri mi dice sempre non consentito.

ecco il codice:
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Multi File Upload</title>
</head>
<body>
<?php

if(isset($_POST['btnSubmit'])){


$allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png");

if(!in_array($_FILES["dynamic"]["type"],$allowed_types)) {

die("Upload non consentito per questo tipo di file. ");

}else
{
             foreach($_FILES['dynamic']['tmp_name'] as $key=>$value)
			{
						move_uploaded_file($value, dirname(__FILE__)."/upload/".$_FILES['dynamic']['name'][$key]);
			}
}
}
		
?>
<form id="form1"  enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p id="upload-area">
   <input name="dynamic[]" id="dynamic" type="file"  size="60" />
</p>

<input name="AddFile" id="AddFile" type="button" value="Add file" onclick="addFileUploadBox()" />
<p><input name="btnSubmit" id="btnSubmit" type="submit" value="upload"></p>
<span id="Span1"  />

<script type="text/javascript">
function addFileUploadBox()
{
    if (!document.getElementById || !document.createElement)
        return false;
        
    var uploadArea = document.getElementById ("upload-area");
    
    if (!uploadArea)
        return;

    var newLine = document.createElement ("br");
    uploadArea.appendChild (newLine);
    
    var newUploadBox = document.createElement ("input");
    
    // Set up the new input for file uploads
    newUploadBox.type = "file";
    newUploadBox.size = "60";
    
    // The new box needs a name and an ID
    if (!addFileUploadBox.lastAssignedId)
        addFileUploadBox.lastAssignedId = 100;
        
    newUploadBox.setAttribute ("id", "dynamic" + addFileUploadBox.lastAssignedId);
    newUploadBox.setAttribute ("name", "dynamic[" + addFileUploadBox.lastAssignedId+"]");
    uploadArea.appendChild (newUploadBox);
    addFileUploadBox.lastAssignedId++;
}
</script>
</form>
</body>
</html>

c'è qualcuno sa dirmi come fare?

grazie mille.
 
ciao
io uso questo metodo
PHP:
<?php
//.....
$tipo=array('jpg', 'JPG','gif','png');//uso questi al posto di image/pjpeg...
foreach($_FILES['immagine']['name'] as $ch => $file){//il tuo sara $_FILES['dynamic']['name']
	$file=pathinfo($file, PATHINFO_BASENAME);//ricavo il nome del file sensa orpelli di percorso
	$estensione=pathinfo($file, PATHINFO_EXTENSION); //ricavo l'estensione del file
	//verifico che l'estensione sia tra i tipi ammessi
	if(!in_array($estensione,$tipo)){
		$non_consentito[]=$file;//per mostrare poi quale sono i non consentiti
	}
}
//poi in funzione del risultato puoi o rimandare al form o non uplodare i file non ammessi
//...
?>
evidentemente devi adattarlo alle tue esigenze
 
ecco il codice funzionante e che controlla il file da uploadare se non è consentito:

lo testato con jpg txt png.

vi dico di provare con altri formati immagini che io non ho nel mio pc.

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Multi File Upload</title>
</head>
<body>
<?php

if(isset($_POST['btnSubmit'])){

			$allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png");


			foreach($_FILES['dynamic']['tmp_name'] as $key=>$value)
			{
				if(!in_array($_FILES['dynamic']['type'][$key],$allowed_types)) 
				{
				echo "Upload non consentito per questo tipo di file. file: ".$_FILES['dynamic']['name'][$key]."<br>";
				}else
				{
					move_uploaded_file($value, dirname(__FILE__)."/upload/".$_FILES['dynamic']['name'][$key]);
				}
			}
		}
		
?>
<form id="form1"  enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p id="upload-area">
   <input name="dynamic[]" id="dynamic" type="file"  size="60" />
</p>

<input name="AddFile" id="AddFile" type="button" value="Add file" onclick="addFileUploadBox()" />
<p><input name="btnSubmit" id="btnSubmit" type="submit" value="upload"></p>
<span id="Span1"  />

<script type="text/javascript">
function addFileUploadBox()
{
    if (!document.getElementById || !document.createElement)
        return false;
        
    var uploadArea = document.getElementById ("upload-area");
    
    if (!uploadArea)
        return;

    var newLine = document.createElement ("br");
    uploadArea.appendChild (newLine);
    
    var newUploadBox = document.createElement ("input");
    
    // Set up the new input for file uploads
    newUploadBox.type = "file";
    newUploadBox.size = "60";
    
    // The new box needs a name and an ID
    if (!addFileUploadBox.lastAssignedId)
        addFileUploadBox.lastAssignedId = 100;
        
    newUploadBox.setAttribute ("id", "dynamic" + addFileUploadBox.lastAssignedId);
    newUploadBox.setAttribute ("name", "dynamic[" + addFileUploadBox.lastAssignedId+"]");
    uploadArea.appendChild (newUploadBox);
    addFileUploadBox.lastAssignedId++;
}
</script>
</form>
</body>
</html>

Prima per uploadare altri file fatte "add file" e selezionate il file e poi fatte upload. e vi carica nella cartella upload/

vi ringrazio molto ..
buona giornata.
 

Discussioni simili