Upload multiple photos using a PHP and MYSQL array It does not work

2

I am trying to upload some pictures through PHP and MYSQL, I am sending by means of a for the images from a form, to receive them is where I imagine my error is, any help I appreciate it.

    <div class="modal fade" id="addphoto_<?php echo $pid; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <center><h4 class="modal-title" id="myModalLabel">Edit Product</h4></center>
            </div>
            <div class="modal-body">
            <div class="container-fluid">
                <?php
                    $a=mysqli_query($conn,"select * from product left join category on category.categoryid=product.categoryid left join supplier on supplier.userid=product.supplierid where productid='$pid'");
                    $b=mysqli_fetch_array($a);
                ?>
                <div style="height:10px;"></div>
                <form role="form" method="POST" action="addphoto.php<?php echo '?id='.$pid; ?>" enctype="multipart/form-data">

                        <?php
                        for ($i = 0; $i < 5; $i++) {
                            print '<div style="height:10px;"></div>                 
                            <div class="form-group input-group">
                            <span class="input-group-addon" style="width:120px;">Photo:</span> 
                            <input type="file" style="width:400px;" class="form-control" name="image">
                            </div>';
                        }
                        ?> 
                   <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Cancel</button>
                <button type="submit" class="btn btn-success"><i class="fa fa-check-square-o"></i> Add Photo</button>
                </form>
            </div>

            </div>

            </div>

    </div>

and this would be the addphoto.php

<?php
include('session.php');
function addphoto (array $fileInfo = PATHINFO($_FILES["image"]["name"])){
    $result = array();
}
$id=$_POST['id'];

while (array =! empty($_FILES["image"]["name"])){

    if ($fileInfo['extension'] == "jpg" OR $fileInfo['extension'] == "png") {
        $newFilename = $fileInfo['filename'] . "_" . time() . "." . $fileInfo['extension'];
        move_uploaded_file($_FILES["image"]["tmp_name"], "../upload/" . $newFilename);
        $location = "upload/" . $newFilename;
    }
    else{
        $location="";
        ?>
            <script>
                window.alert('Photo not added. Please upload JPG or PNG photo only!');
            </script>
        <?php
    }

        mysqli_query($conn,"insert into carousel (productid,photo) values ('$id','$location')");
?>
    <script>
        window.alert('Product added successfully!');
        window.history.back();
    </script>
<?php

? >

}

I miss this error:

Parse error: syntax error, unexpected '(', expecting ')' in C: \ xampp \ htdocs \ FCH-master \ POS \ admin \ addphoto.php on line 3

Thank you very much for the help.

    
asked by José Luis Molina Cascante 30.07.2018 в 18:09
source

1 answer

1

I declared the input as array "files []" in the form I send it by url

and in addphoto.php I do not work

<?php
    include('session.php');

    $id=$_GET['id'];

    //Como el elemento es un arreglos utilizamos foreach para extraer todos los valores
    foreach($_FILES["archivo"]['tmp_name'] as $key => $tmp_name)
    {
        //Validamos que el archivo exista
        if($_FILES["archivo"]["name"][$key]) {
            $filename = $_FILES["archivo"]["name"][$key]; //Obtenemos el nombre original del archivo
            $source = $_FILES["archivo"]["tmp_name"][$key]; //Obtenemos un nombre temporal del archivo

            $directorio = 'upload'; //Declaramos un  variable con la ruta donde guardaremos los archivos

            //Validamos si la ruta de destino existe, en caso de no existir la creamos
            if(!file_exists($directorio)){
                mkdir($directorio, 0777) or die("No se puede crear el directorio de extracci&oacute;n");    
            }

            $dir=opendir($directorio); //Abrimos el directorio de destino
            $target_path = $directorio.'/'.$filename; //Indicamos la ruta de destino, así como el nombre del archivo

            //Movemos y validamos que el archivo se haya cargado correctamente
            //El primer campo es el origen y el segundo el destino
            if(move_uploaded_file($source, $target_path)) { 
                echo "El archivo $filename se ha almacenado en forma exitosa.<br>";
                } else {    
                echo "Ha ocurrido un error, por favor inténtelo de nuevo.<br>";
            }
            closedir($dir); //Cerramos el directorio de destino


             mysqli_query($conn,"call GuardarImagen('$id','$target_path')"); 
             $pid=mysqli_insert_id($conn);


        }
    }
    ?>
    <script>
            window.alert('Product added successfully!');
            window.history.back();
        </script>

?>
    
answered by 01.10.2018 / 07:39
source