简体   繁体   中英

Upload multiple files in PHP failed

I want to upload multiple files in a single submit button using PHP but its not working and i had tried upload a single file using php and it was working well so i thought that it would be good to just copy the same code for the second file to have two files uploaded but in vain.

i had tried this solution on SO but i had no success : solution1

solution2

solution3

    <form action="" method="post" target="frame" enctype = "multipart/form-data">
        <div class="buttonsend">
            <p>Entrez votre fichier.cfg :</p>
            <input type = "file" name = "cfg" /></br>
            <p>Entrez votre fichier.dat :</p>
            <input type = "file" name = "dat" /></br></br> 
            <button id="send" type="submit" name="send">Générer</button>
        </div>
    </form>

    <?php 
        if (isset($_POST['send'])) {
            $file_tmpcfg = $_FILES['cfg']['tmp_name'];
            $file_namecfg = $_FILES['cfg']['name'];
            move_uploaded_file($file_tmpcfg,"/home/imagesdcard/www/".$file_namecfg);
            $file_tmpdat = $_FILES['dat']['tmp_name'];
            $file_namedat = $_FILES['dat']['name'];
            move_uploaded_file($file_tmpdat,"/home/imagesdcard/www/".$file_namedat);
        }
    ?>

It is working fine. I printed uploaded files and it didnt show any error.

        <form action="" method="post" target="frame" enctype = "multipart/form-data">
    <div class="buttonsend">
        <p>Entrez votre fichier.cfg :</p>
        <input type = "file" name = "cfg" /></br>
        <p>Entrez votre fichier.dat :</p>
        <input type = "file" name = "dat" /></br></br> 
        <button id="send" type="submit" name="send">Générer</button>
    </div>
</form>

<?php 
    if (isset($_POST['send'])) {

    $uploaddir = '/var/www/html/';
    $uploadfileCfg = $uploaddir . basename($_FILES['cfg']['name']);
    $uploadfileDat = $uploaddir . basename($_FILES['dat']['name']);

    echo "<p>";

    if (move_uploaded_file($_FILES['cfg']['tmp_name'], $uploadfileCfg)) {
      echo "File cfg is valid, and was successfully uploaded.\n<br>";
    }else{
        echo "File cfg is invalid";
    }
    if (move_uploaded_file($_FILES['dat']['tmp_name'], $uploadfileDat)) {
      echo "File dat is valid, and was successfully uploaded.\n<br>";
    }else{
        echo "File dat is invalid";
    }

    echo "</p>";
    echo '<pre>';
    echo 'Here is some more debugging info:<br>';
    print_r($_FILES);
    print "</pre>";
}
?>

result:

File cfg is valid, and was successfully uploaded. 
File dat is valid, and was successfully uploaded. 

Here is some more debugging info:
Array
(
    [cfg] => Array
        (
            [name] => 102440.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpQdhjY0
            [error] => 0
            [size] => 72469
        )

    [dat] => Array
        (
            [name] => 1plus1.pem
            [type] => application/x-x509-ca-cert
            [tmp_name] => /tmp/phpId9rW1
            [error] => 0
            [size] => 1692
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM