Upload 4 file php

Maxi_007

Nuovo Utente
22 Ott 2018
10
0
1
Buonasera, devo fare l'upload di 4 file da form
HTML:
HTML:
<div class="row">
                                <div class="small-2 columns">
                                    <label for="FOTO1" class="right inline">FOTO 1:</label>
                                </div>
                                <div class="small-10 columns">
                                    <input type="file" id="userfile" name="userfile">
                                </div>
                            </div>
                            <div class="row">
                                <div class="small-2 columns">
                                    <label for="FOTO2" class="right inline">FOTO 2:</label>
                                </div>
                                <div class="small-10 columns">
                                    <input type="file" id="FOTO2" name="FOTO2" value="<%=(T_Tab.Fields("FOTO2"))%>">
                                </div>
                            </div>
                            <div class="row">
                                <div class="small-2 columns">
                                    <label for="FOTO3" class="right inline">FOTO 3:</label>
                                </div>
                                <div class="small-10 columns">
                                    <input type="file" id="FOTO3" name="FOTO3" value="<%=(T_Tab.Fields("FOTO3"))%>">
                                </div>
                            </div>           
                            <div class="row">
                                <div class="small-2 columns">
                                    <label for="FOTO4" class="right inline">FOTO 4:</label>
                                </div>
                                <div class="small-10 columns">
                                    <input type="file" id="FOTO4" name="FOTO4" value="<%=(T_Tab.Fields("FOTO4"))%>">
                                </div>
                            </div>
Qui codice php che esegue upload di un file:
PHP:
<?php
if (!isset($_FILES['userfile']) || !is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  echo 'Non hai inviato nessun file...';
  exit;   
}
$uploaddir = 'D:/inetpub/webs/public/prodotti/';
$userfile_tmp = $_FILES['userfile']['tmp_name'];
$userfile_name = $_FILES['userfile']['name'];
if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
  echo 'File inviato con successo.';
}else{
  echo 'Upload NON valido!';
}
header("location: /gestione/login_prodotti.asp");
?>
come faccio l'upload degli altri 4 file?
 
PHP:
foreach($_FILES as $key=>$file){
$userfile_tmp = $_FILES[$key]['tmp_name'];
$userfile_name = $_FILES[$key]['name'];
if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
  echo 'File inviato con successo.';
}else{
  echo 'Upload NON valido!';
}
}
 
Ho inserito come mi hai suggerito, ma non esegue nessun upload.
HTML:
<form action="/public/upload.php" method="post" enctype="multipart/formdata">
    <div class="row">
        <div class="small-2 columns">
            <label for="FOTO1" class="right inline">FOTO 1:</label>
        </div>
        <div class="small-10 columns">
            <input type="file" id="userfile" name="userfile">
        </div>
    </div>
    <div class="row">
        <div class="small-2 columns">
            <label for="userfile" class="right inline">FOTO 2:</label>
        </div>
        <div class="small-10 columns">
            <input type="file" id="userfile" name="userfile">
        </div>
    </div>
    <div class="row">
        <div class="small-2 columns">
            <label for="userfile" class="right inline">FOTO 3:</label>
        </div>
        <div class="small-10 columns">
            <input type="file" id="userfile" name="userfile">
        </div>
    </div>           
    <div class="row">
        <div class="small-2 columns">
            <label for="userfile" class="right inline">FOTO 4:</label>
        </div>
        <div class="small-10 columns">
            <input type="file" id="userfile" name="userfile">
        </div>
    </div>
    <div class="row">
        <div class="small-12 large-centered columns">
            <input type="submit" name="upload" value="UPLOAD" class="button radius">
        </div>
    </div>
</form>

PHP:
<?php
$uploaddir = 'D:/inetpub/webs/public/prodotti/';
$userfile_tmp = $_FILES['userfile']['tmp_name'];
$userfile_name = $_FILES['userfile']['name'];
foreach($_FILES as $key=>$file){
    $userfile_tmp = $_FILES[$key]['tmp_name'];
    $userfile_name = $_FILES[$key]['name'];
    if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
          echo 'File inviato con successo.';
    }
    else{
          echo 'Upload NON valido!';
    }
}
//header("location: /gestione/login_prodotti.asp");
?>
 
<form action="/public/upload.php" method="post" enctype="multipart/formdata"> <div class="row"> <div class="small-2 columns"> <label for="FOTO1" class="right inline">FOTO 1:</label> </div> <div class="small-10 columns"> <input type="file" id="userfile" name="userfile"> </div> </div> <div class="row"> <div class="small-2 columns"> <label for="userfile" class="right inline">FOTO 2:</label> </div> <div class="small-10 columns"> <input type="file" id="userfile" name="userfile"> </div> </div> <div class="row"> <div class="small-2 columns"> <label for="userfile" class="right inline">FOTO 3:</label> </div> <div class="small-10 columns"> <input type="file" id="userfile" name="userfile"> </div> </div> <div class="row"> <div class="small-2 columns"> <label for="userfile" class="right inline">FOTO 4:</label> </div> <div class="small-10 columns"> <input type="file" id="userfile" name="userfile"> </div> </div> <div class="row"> <div class="small-12 large-centered columns"> <input type="submit" name="upload" value="UPLOAD" class="button radius"> </div> </div> </form>
?
Codice:
 <input type="file" id="userfile" name="userfile[]">
 
Non è possibile uploadare foto per foto? Nel senso scegliere una foto1 e assegnarla a foto1 ecc.ecc?
Ho comunque modificato come consigliato da te, ma i file non vengono uploadati.
HTML:
HTML:
<form action="/public/upload.php" method="post" enctype="multipart/formdata">
                        <div class="row">
                            <div class="small-2 columns">
                                <label for="FOTO1" class="right inline">FOTO DA UPLOADARE:</label>
                            </div>
                            <div class="small-10 columns">
                                <input type="file" id="file" name="file[]" multiple>
                            </div>
                        </div>   
                        <div class="row">
                            <div class="small-12 large-centered columns">
                                <input type="submit" name="upload" value="UPLOAD" class="button radius">
                            </div>
                        </div>
                    </form>
PHP:
PHP:
<?php
    $folder = 'D:/inetpub/webs/public/prodotti/';
    if(isset($_FILES['file']['name'])){
        $count=0;
        foreach ($_FILES['file']['name'] as $filename) {
            $destination=$folder;
            $origin=$_FILES['file']['tmp_name'][$count];
            $count++;
            $destination=$destination.basename($filename);
            move_uploaded_file($origin, $destination);
        }
    }
    //header("location: /gestione/login_prodotti.asp");
?>
 
Ultima modifica:
si hai ragione, non ho eliminato tutto il vecchio script, ma non capisco, ho messo quello che mi hai suggerito tu e non funziona ne con un file ne con 2-3-4-5 o più.
html:
HTML:
<form action="/public/upload.php" method="post" enctype="multipart/formdata">
                        <div class="row">
                            <div class="small-2 columns">
                                <label for="FOTO1" class="right inline">FOTO DA UPLOADARE:</label>
                            </div>
                            <div class="small-10 columns">
                                <input type="file" id="userfile" name="userfile[]">
                            </div>
                        </div>   
                        <div class="row">
                            <div class="small-12 large-centered columns">
                                <input type="submit" name="upload" value="UPLOAD" class="button radius">
                            </div>
                        </div>
                    </form>
PHP:
PHP:
<?php
$uploaddir = 'D:/inetpub/webs/public/prodotti/';;
$userfile_tmp = $_FILES['userfile']['tmp_name'];
$userfile_name = $_FILES['userfile']['name'];
foreach($_FILES as $key=>$file){
    $userfile_tmp = $_FILES[$key]['tmp_name'];
    $userfile_name = $_FILES[$key]['name'];
    if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
          echo 'File inviato con successo.';
    }
    else{
          echo 'Upload NON valido!';
    }
}
//header("location: /gestione/login_prodotti.asp");
?>
 
Ho capito qual'era il problema, mancava il trattino in form-data.
ora mi da questo errore:
Warning: move_uploaded_file() expects parameter 1 to be string, array given in D:\inetpub\webs\public\upload.php on line 8
Upload NON valido!
PHP:
PHP:
<?php
$uploaddir = 'D:/inetpub/webs/public/prodotti/';
$userfile_tmp = $_FILES['userfile']['tmp_name'];
$userfile_name = $_FILES['userfile']['name'];
foreach($_FILES as $key=>$file){
    $userfile_tmp = $_FILES[$key]['tmp_name'];
    $userfile_name = $_FILES[$key]['name'];
    if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
          echo 'File inviato con successo.';
    }
    else{
          echo 'Upload NON valido!';
    }
}
//header("location: /gestione/login_prodotti.asp");
?>
 
Scusa si chiamano tutti "userfile"?
Se è così nel foreach devi aggiungere un ciclo ulteriore
PHP:
foreach($_FILES as $kk=>$vv){
foreach($vv as $key=>$file){
 $userfile_tmp = $_FILES[$key]['tmp_name'];
    $userfile_name = $_FILES[$key]['name'];
    if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
          echo 'File inviato con successo.';
    }
    else{
          echo 'Upload NON valido!';
    }
}
}
 
In che senso, i file o gli input?
comunque ho fatto le modifiche che mi hai suggerito e ora mi da upload non valido per un paio di volte.
PHP:
<?php
$uploaddir = 'D:/inetpub/webs/public/prodotti/';
$userfile_tmp = $_FILES['userfile']['tmp_name'];
$userfile_name = $_FILES['userfile']['name'];
foreach($_FILES as $kk=>$vv){
    foreach($vv as $key=>$file){
         $userfile_tmp = $_FILES[$key]['tmp_name'];
        $userfile_name = $_FILES[$key]['name'];
        if (move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name)) {
              echo 'File inviato con successo.';
        }
        else{
              echo 'Upload NON valido!';
        }
    }
}
//header("location: /gestione/login_prodotti.asp");
?>
 

Discussioni simili