upload multiple images at the same time to feed a slide

Asked

Viewed 6,233 times

0

I am currently trying to upload several images at the same time to the table images, these have to be stored all together or within an array right? in such a way (http://gyazo.com/237d0e2107dcd90c388b139c64a6b4a6), 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:(http://gyazo.com/81566817c75b802e670352031ecbc00b) basically what I want is inside the picture table to 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");
    }
  • I didn’t understand the problem.

  • i want to add projects in which each project has the corresponding images, then on the site on the homepage appears the main image of the project and when I click on it sends me to projects.php where it has the project information and a Carousel to pass the images I entered, but what I need is to add the images in each project.

  • first project add->table images-> introduces a set of images->imagem1.png,imagem2.png,imagem3.png

2 answers

2

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

$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);
  • Lucas thanks a lot worked :The thank you even do not know the turns I walked with this, tell me one more thing, now the code I have to get the images to be shown on the slide should not be working can you take a look? http://pastebin.com/eqq9q1rY thank you

  • Change the first parameter of explode for a ";" (semicolon)

  • yes I just did, but I have a problem, for example 5 images I upload only 2/3 appear, never appear all

  • Make sure the images have been saved and everything is in the right place.

  • in the database are all images but in the folder only some, are not all that I entered

  • Lucas already solved the problem, the ";" was giving error I had to put "," but now I have another problem could help me?

  • If you changed in the explode, you have to change in the implode too and vice versa.

  • yes this is already done, my problem is the following : in index now I will have to show an image of the array I chose to be the first, so when clicking on the image go to the page projects.php where you have the information and the slide to pass the images, but the code I have just shows me an image that I added is add 2 images http://gyazo.com/380da2fab2c790df93c4587ed37bab31 and on the website it only showed me 1 http://gyazo.com/ecf6e19fa456e0e5f984f64580d46265 the thought this one here http://pastebin.com/pyd1p6iD

  • In this code you are only taking the first image (index 0 of the array). If you want to display them all, loop $imagem_arry.

Show 4 more comments

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 multi-valued attribute.

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

  • I already got thank you :)

  • I recommend filtering the filename and replacing special characters: à or po a, spaces with _ or -, etc.

Browser other questions tagged

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