As I keep name of the images in a BD Mysql

0

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);
    }
    ?>
    
asked by Yii 14.04.2017 в 19:52
source

1 answer

0

What is happening is that at the moment of moving the photo to the directory you are re-naming it. To solve your problem of saving the data in the database you can do it in two ways:

  
  • Maintaining the same name as the image is uploaded.
  •   
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
        $i = 0; 
        //  Loop $_FILES to exeicute all files
        foreach ($_FILES['userImage']['name'] as $f => $name) { 
    
            $lid= $_FILES['userImage']['name'][$f];
    
            $array[$i]= $lid;
    
            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                
                } 
               $i++          
            }   
        }  
    
      

    In the Insert it would be

                $Sql="insert into jugador (imagen1,imagen2,imagen3) values(
                        '".$array[0]."',
                        '".$array[1]."',
                        '".$array[2]."')";
    
    
    
                mysql_query($Sql);
    
      
  • Placing the renamed name in the database:
  •   
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
        $i = 0;
        //  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;
    
            $array[$i]= $lid;
    
            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                
                }  
              $i++;         
            }   
        } 
    
      

    The Insert 2

            $Sql="insert into jugador (imagen1,imagen2,imagen3) values(
                    '".$array[0]."',
                    '".$array[1]."',
                    '".$array[2]."')";
    
    
    
            mysql_query($Sql);
    
        
    answered by 15.04.2017 / 22:58
    source