[PHP] Caricamento file multiplo

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
Salve, ho scritto questo mini codice:
Codice:
<!DOCTYPE html>
<html>
<head>
    <title>PHP - Multiple upload using dropzone.js</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/dropzone.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/dropzone.js" charset="utf-8"></script>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h2>PHP - Multiple upload using dropzone.js</h2>
            <form action="upload.php" enctype="multipart/form-data" class="dropzone" id="image-upload">
                <div>
                    <h3>Upload Multiple By Click On Box</h3>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    Dropzone.options.imageUpload = {
        maxFilesize:100,
        uploadMultiple: true,
        parallelUploads: 1,
        acceptedFiles: ".jpeg,.jpg,.png,.gif,.zip"
    };
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.1/umd/popper.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>

e il suo test php è questo:
PHP:
<?php

// Check for errors
if($_FILES['file']['error'] > 0){
    die('An error ocurred when uploading.');
}


// Check if the file exists
if(file_exists('uploads/' . $_FILES['file']['name'])){
    die('File with that name already exists.');
}

// Upload file
if(!move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name'])){
    die('Error uploading file - check destination is writeable.');
}

die('File uploaded successfully.');

non da nessun errore ma non carica nulla. Avete idea come mai ? uso la libreria dropzone.js:
http://www.dropzonejs.com/

Mi potete dire come mai?
Grazie mille e buona settimana.
 
Ciao @luigi777 , Hai sbagliato la conf del file upload.php...
Se stai caricando più file, viene passato un'array... quindi riscrivendo il file upload esce così.... (naturalmente devi crearti le eccezioni del caso)...
PHP:
<?php
/**
 * Created by PhpS.
 * User: Marco Arena Di R.
 * Date: 17/03/2018
 * Time: 19:48
 */

$response=[];
if(isset($_FILES['file'])){
    if(isset($_FILES['file']['name'])){
        foreach ($_FILES['file']['name'] as $k=>$v){

            try{
                move_uploaded_file($_FILES['file']['tmp_name'][$k],'uploads/'.$v);
                $response[]=['code'=>200,'status'=>'success'];
            }catch (Exception $e){
                //var_dump($e);
                $response[]=['code'=>404,'status'=>'error'];
            }

        }

    }
}
 
ok, scusa .. ma nel frattempo ho risolto cosi:
Codice:
<!DOCTYPE html>
<html>
<head>
    <title>PHP - Multiple upload using dropzone.js</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/dropzone.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/dropzone.js" charset="utf-8"></script>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h2>PHP - Multiple upload using dropzone.js</h2>          
            <form action="upload.php" class="dropzone"></form>
        </div>
    </div>
</div>

<script type="text/javascript">
    Dropzone.options.imageUpload = {
        url: 'upload.php',
        maxFilesize:100,
        uploadMultiple: true,
        parallelUploads: 1,
        acceptedFiles: ".jpeg,.jpg,.png,.gif,.zip"
    };
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.1/umd/popper.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>

---
<?php
$ds          = DIRECTORY_SEPARATOR;  //1
 
$storeFolder = 'uploads';   //2
 
if (!empty($_FILES)) {
   
    $tempFile = $_FILES['file']['tmp_name'];          //3            
     
    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4
   
    $targetFile =  $targetPath. $_FILES['file']['name'];  //5
 
    move_uploaded_file($tempFile,$targetFile); //6
   
}
?>
e funziona alla grande tranne la mia linea adsl che per una foto da 5.mb ci sta quasi 20 minuti.

ti ringrazio molto e buona serata.
 
if (!empty($_FILES)) { $tempFile = $_FILES['file']['tmp_name']; //3 $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //4 $targetFile = $targetPath. $_FILES['file']['name']; //5 move_uploaded_file($tempFile,$targetFile); //6 }
Ocio a quello che hai scritto..... Controlla se esiste il file name, non verificare se esiste solo FILES... ;)
 

Discussioni simili