How to "merge" foreach results

Asked

Viewed 134 times

0

I have a script to upload multiple files in php. Then when uploading, I wanted him to insert in the table photos, the information with the names in this format: "name1.jpg|Nome2.jpg".

The query I use to insert is this:

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

Below the upload script. I need it (concatene) 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';
}
  • And that line didn’t work? $name=$idlancamento.'_'.$name; Try to start $name

  • just solved, put: $Names = . $name; then at the end I use $Names

1 answer

1

I solved by adding the line $names.=$name.'|'; and use the Insert with $Names value after all

$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';
}

Browser other questions tagged

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