Can I create a new session with the form tag?

0

Hello, I'm trying to create a simulation in the Site assembly, and for that I've already been able to simulate image exchange through $ _SESSION.

I created this $ _SESSION below to load the first image of a banner, and to allow the user to exchange the image, through a form and an image upload file.

// Inicio da Sessão de Upload de Imagem.
if(!isset($_SESSION['banner01'])){ // Se a Session não for iniciada
    $img01 = 'img_banner/01.png'; // Carrega essa imagem já pré determinada
}else{ // Se não
if(isset($_SESSION)) { // Se a Session for iniciada
    $img01 = ''.$_SESSION['banner01'].''; // Carrega a imagem selecionada pelo usuario através do formulário de upload de imagem.
}}

The form used is this below:

<form action="recebe_upload_01.php" method="POST" enctype="multipart/form-data">
<label>Selecione uma nova Imagem:</label><br />
<input type="file" name="img01[]" accept="image/*" ><br /><br />
<input type="submit" name="img01" value="Atualizar">
</form>

And the upload file used is this below:

<?php
@session_start(); // Inicia a session.

if(isset( $_POST['img01'] ) ){
    //INFO IMAGEM   
    $file = $_FILES['img01'];
    $numFile = count( array_filter( $file['name'] ) );
    //PASTA
    $folder = '../img_simulador';
    //REQUiSITOS
    $permite = array( 'image/jpeg', 'image/png', 'image/gif' );
    $maxSize = 1024 * 1024 * 5;
    //MENSAGEM
    $msg = array();
    $errorMsg = array(
        1 => 'O arquivo no upload é maior que o Limite de finido em upload_maxsize',
        2 => 'O arquivo ultrapassa o limite de tamanho em Max_file_size',
        3 => 'O upload do arquivo foi feito parcialmente',
        4 => 'Não foi feito o upload do arquivo',
    );
    if($numFile <= 0){
        echo 'Selecione uma Imagem!!';
    }else{
        for($i = 0; $i < $numFile; $i++){
            $name = $file['name'][$i];
            $type = $file['type'][$i];
            $size = $file['size'][$i];
            $error = $file['error'][$i];
            $tmp = $file['tmp_name'][$i];

            $extensao = @end(explode('.', $name));
            $novoNome = rand().".$extensao";

            //Se você quer várias mensagens não use else.
            if($error!=0){
                $msg[] = "Erro: {$errorMsg[$error]}! Nome do arquivo: {$name}.";
            }if(!in_array($type, $permite)){
                $msg[] = "Tipo do arquivo inválido! Nome do arquivo: {$name}.";
            }if($size > $maxSize){
                $msg[] = "Tamanho do arquivo muito grande! Nome do arquivo: {$name}.";
            }else{
                $destino = $folder."/".$novoNome;
                if(move_uploaded_file($tmp, $destino)){
                    $_SESSION['banner01'] = $destino;

            echo "<meta http-equiv='refresh' content='0; URL= inicial_banner.php'>
            <script language='javascript'>
            window.alert('Imagem atualizada com sucesso!');
            </script>";
            }}}}}
?>

My question is if I have to insert one or more images using a form tag to add them. In other words, creating new $ _SESSION with the names of $ _SESSION ['banner02'], 03, 04 and so on, or even with random names, and the images being stored in the folder "$ folder = '../img_simulator '; ", as determined in the image upload file.

This $ _SESSION would have to be temporary, because when the user logs out, or even close the browser they would be destroyed, leaving only the image already predetermined in a future page opening.

I do not know if I could express myself comprehensively what I need, but I hope that the friends have understood my doubts!

I wonder how?

And how could it be done?

A big hug to everyone, and waiting for good tips.

    
asked by anonymous 13.06.2016 / 16:48

1 answer

0

I do not quite understand your ultimate goal, but for $_SESSION , there is only one for each SESSION. What happens is that you can destroy it unset ($_SESSION) and reboot again with other values. But I think you do not want this, after all you will lose all information regarding the session. A likely solution would be to run SESSIONS within SESSIONS and you can access them whenever you want. Here's how:

$_SESSION["session01"]=Grave aqui o que quiser gravar, (uma array ou qualquer coisa),
$_SESSION["session02"] =Grave aqui o que quiser gravar, (Outra array ou qualquer outra coisa).

When you no longer need each of them, simply delete them:

unset ($_SESSION["session01"]) // Deletes session information "session01".

So you record different information and later destroy what needs to be destroyed, and keep the main session until the user logs out.

I hope it does.

    
13.06.2016 / 17:44