Image is being uploaded without extension

0

I put a PHP code to upload images, the images are not being saved in the directory with the code below:

$post_id = $db->insert($query);
$ext = explode('.', $_FILES['img']['name']);
$path = "../content/img/".$post_id.".".$ext[count($ext)]-1;

$img = move_uploaded_file( $_FILES["img"]["tmp_name"], $path);

var_dump $ path int -1

$ ext var_dump

     array (size=2)
  0 => string 'nome-da-imagem' (length=14)
  1 => string 'jpg' (length=3)
(Já que é uma imagem jpg)

var_dump $ img boolean true

But when I modify the $ path variable to only $path = "../content/img/".$post_id; var_dump gives string '../content/img/60' (length=17)

It saves (60 because it is the ID of this new post), but without the extension.

    
asked by anonymous 19.09.2015 / 20:08

1 answer

0

Try using it like this:

$path = "../content/img/".$post_id.".".$ext[count($ext)-1]; # o -1 dentro do índice assim ele diminui com o count da variável.

Or:

$path = "../content/img/".$post_id.".".$ext[1];
    
19.09.2015 / 21:07