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
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 change the $path variable to only $path = "../content/img/".$post_id;
var_dump gives string '../content/img/60' (length=17)
There saves (60 because it is the ID of this new post), but without the extension.
You have already tried using $ext[1] since it returns an array. Pq by var_dump it returns two positions 0 and 1 to regain some position you need to use $ext[0] or $ext[1].
– Thalles Daniel
The -1 you are using needs to stay inside the Dice and not outside $path = ".. /content/img/". $post_id.".". $ext[Count($ext)-1];
– Thalles Daniel
Both ways worked out!!
– Geovani C
If it worked consider the answer I put below
– Thalles Daniel
One suggestion is to protect the upload pq vc by opening an access to the server and then if someone goes up a php script they already know :( . make restrictions with the extension type, size, encryption with sha1 or whatever you choose etc.
– Thalles Daniel
At the moment I’m creating the features, I really need to make these restrictions, thanks for the touch.
– Geovani C
@Geovanic, there is better alternative to explode, use the function pathinfo
– Williams