upload multiple pictures at the same time to feed a slide

0

I'm currently trying to do is upload multiple images at the same time to the images table, does this have to be stored all together ie within a certain array? in order to stay that way ( link ), my problem is that the code I have makes me feel like I have to insert one by one to appear on the site like this :( link ) basically what I want is inside the image table store an image that contains several images to feed a slide.

$fdata = $_FILES['img'];
    $uploads_dir = '/upload';
    //echo count($fdata['name']);
    for($i = 0; $i < count($fdata['name']); ++$i)
    {

  $data=date("ymd");
  $query=("insert into 'portfolio_imagens' ('nome', 'descricao','cliente','imagem','publicacao') VALUES ('".$_POST['nome']."','".$_POST['descricao']."','".$_POST['cliente']."','".$name."','".$data."')");   $result=mysql_query($query);
        $name = $_FILES['img']['name'][$i];
        $temp_name = $_FILES['img']['tmp_name'][$i];
        move_uploaded_file($temp_name, "$uploads_dir"."$name");
    }
    
asked by anonymous 23.07.2014 / 12:48

2 answers

2

You should leave the SQL out of the loop and inside it, upload the images and store the names in an array to insert into the database after:

$fdata = $_FILES['img'];
$uploads_dir = '/upload';
$img = array();
for ($i = 0; $i < count($fdata['name']); $i++) {
    $name = $_FILES['img']['name'][$i];
    $temp_name = $_FILES['img']['tmp_name'][$i];
    move_uploaded_file($temp_name, "$uploads_dir"."$name");
    $img[] = $name;
}
$data = date("ymd");
$img = implode(';', $img);
$query = "INSERT INTO 'portfolio_imagens' ('nome', 'descricao','cliente','imagem','publicacao') VALUES ('".$_POST['nome']."', '".$_POST['descricao']."', '".$_POST['cliente']."', '".$img."', '".$data."')";
mysql_query($query);
    
23.07.2014 / 13:15
0
$images = $_FILES['img'];

$uploads_dir = '/upload';

$img = array();

foreach($images as $image)
{
  $name = $image['name'];
  $temp_name = $image['tmp_name'];
  if(move_uploaded_file($temp_name, "$uploads_dir.$name"))
  {
    //O arquivo foi movido para o destino com sucesso.
    // Você não vai querer registrar o nome de uma imagem que não existe na pasta uploads
    $img[] = $name;
  }
}

$date = date("Ymd");

$img = implode(';', $img);

$query = "INSERT INTO 'portfolio_imagens' ('nome', 'descricao','cliente','imagem','publicacao') VALUES ('".$_POST['nome']."', '".$_POST['descricao']."', '".$_POST['cliente']."', '".$img."', '".$date."')";

mysql_query($query);

As I understand it, the image attribute of the portfolio_images table is a multivalued attribute.

Just a hint: Do not use the mysql extension of php. Instead use mysqli or pdo_mysql. mysql is old and has a number of security holes; To change mysql to mysql, just add 'i', since most functions have the same name, with the addition of 'i'.

    
24.07.2014 / 01:32