How to "join" foreach results

0

I have a script to do multiple upload files in php. At the time of uploading, he wanted him to insert into the photos table the information with the names in that format: "name1.jpg | name2.jpg".

The query I use to insert is this:

INSERT INTO foto (idlancamento, fotos)
VALUES ($idlancamento, $nomes);

Below the upload script. I need it (concatenate) the names to play in a variable $nomes :

$idlancamento=$_GET['idlancamento'];
echo '
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
        <input type="submit" value="Upload!">
    </form>
';

$format_file = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1000; //TAMANHO MÁXIMO
mkdir("/".$idlancamento, 0700);
$path = "/".$idlancamento."/"; // PASTA
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES para executar todos arquivos
    foreach ($_FILES['files']['name'] as $f => $name) {
        //cria nome do arquivo com o id do lançamento
        $name=$idlancamento.'_'.$name;
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $format_file) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; // Number of successfully uploaded file
            }
        }
    }
    echo 'upload de '.$count.' arquivos';
}
    
asked by anonymous 15.06.2018 / 21:54

1 answer

1

I solved by adding the line $names.=$name.'|'; and use the insert with value $ names after everything

$idlancamento=$_GET['idlancamento'];
echo '
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
        <input type="submit" value="Upload!">
    </form>
';

$format_file = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1000; //TAMANHO MÁXIMO
mkdir("/".$idlancamento, 0700);
$path = "/".$idlancamento."/"; // PASTA
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES para executar todos arquivos
    foreach ($_FILES['files']['name'] as $f => $name) {
        //cria nome do arquivo com o id do lançamento
        $name=$idlancamento.'_'.$name;
        $names.=$name.'|';
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $format_file) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; // Number of successfully uploaded file
            }
        }
    }
    echo 'upload de '.$count.' arquivos';
}
    
15.06.2018 / 22:06