Could you help me with this?
I have a form in html with 3 inputs to upload photos, it works perfect but now I want to save the names of the images uploaded in a bd (when I upload these images are renamed without losing their extension) and my request is if you could help me with the code to insert them in a "row" of my table "Player", I have already tried to save them but I do not know how to extract the new names and then save them, I do not know how to store them in a variable
PS: the images can be jpg, png or different extensions, and you have to save with your extension, it is not necessary to upload the 3 images sometimes only upload 2 or 1
TABLA JUGADOR
---------------------------------------------------------
| id | nombre | apellidos | foto1 | foto2 | foto3 |
--------------------------------------------------------
| 1 | juan | perez |ac.jpg | xd.png| asd.gif|
--------------------------------------------------------
PHP Code
<?php
include ('conexion.php');
function generarCodigo($longitud) {
$key = '';
$pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
$max = strlen($pattern)-1;
for ($i=0;$i < $longitud;$i++) $key .= $pattern{
mt_rand(0,$max)
};
return $key;
}
$valid_formats = array("jpg", "png","JPG","jpeg","JPEG","PNG","gif");
$max_file_size = 1024*1000;
//100 kb
$path = "images/";
// Upload directory
$count = 0;
$letragenerada = generarCodigo(6);
$random=rand(1,10);
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['userImage']['name'] as $f => $name) {
$ext = pathinfo($_FILES['userImage']['name'][$f], PATHINFO_EXTENSION);
$lid= $letragenerada."".$random.".".$ext;
if ($_FILES['userImage']['error'][$f] == 4) {
continue;
// Skip file if any error found
}
if ($_FILES['userImage']['error'][$f] == 0) {
if ($_FILES['userImage']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue;
// Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue;
// Skip invalid file formats
}
else{
// No error found! Move uploaded files
if(move_uploaded_file($_FILES["userImage"]["tmp_name"][$f], $path.$lid))
$count++;
// Number of successfully uploaded file
}
}
}
// Supongo que aqui estaria el codigo para insertar, no se como poner los nombres en values !
$imagenes = implode(",",$_FILES['userImage']['name']);
$array = explode(",", $imagenes);
$Sql="insert into jugador (imagen1,imagen2,imagen3) values(
'".$array[0]."',
'".$array[1]."',
'".$array[2]."')";
mysql_query($Sql);
}
?>