How to capture a $_FILE file name and print to an echo

Asked

Viewed 225 times

0

<form action="upload2.php" method="post" enctype="multipart/form-data">
<input type="file" name="arquivo[]" multiple="multiple"/><br/><br/>
<input type="submit" name="enviar" value="Enviar"/>

upload2.php

<?php
$diretorio = "img/";

if(!is_dir($diretorio)){
echo "pasta $diretorio não existe";
}else{
$arquivo =isset($_FILES['arquivo'])?$_FILES['arquivo'] : FALSE;

for($controle = 0; $controle<count($arquivo['name']);$controle++){
$destino = $diretorio."/".$arquivo['name'][$controle];

    if (move_uploaded_file($arquivo['tmp_name'][$controle],$destino)){
        echo "upload realizado com sucesso<br><br>";

    }
    else{
        echo "erro ao realizar upload";
    }
}
}
  • echo $_FILES['arquivo']['name']?

  • for me gave this error Notice: Array to string Conversion in C: wamp www upload_files upload2.php on line 20

  • $destination = $directory." /". $file.... vai gerar img// ..... two-bar

  • echo $file['name'][$control];

  • maybe the error is in the input name name="arquivo[]". It is correct to use [] in the name?

1 answer

0


The point is that using <input type="file" multiple="multiple" name="upload[]"> the PHP will receive the array like this:

$_FILES['upload']['name'][n]

Where n is equivalent to the number of files added by <input>. Then to print the names of the files that were selected, you can use a for():

$numero_de_arquivos = count($_FILES['upload']['name']); // conta o número de arquivos adicionados
for($i = 0; $i < $numero_de_arquivos; $i++){
    echo $_FILES['upload']['name'][$i]; // aqui ele pega o nome de cada arquivo e imprime com o echo
    echo "<br>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.