Is it possible to store the tmp_name of a file in cookies?

Asked

Viewed 35 times

2

I have the following code

$allowed = array('png', 'jpg', 'gif','zip');

    if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

        $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

        if(!in_array(strtolower($extension), $allowed)){
            echo '{"status":"error"}';
            exit;
        }
        else{

            $nomesImagens = (isset($_COOKIE['nomesImagens'])) ? $_COOKIE['nomesImagens'] : '';

            if (empty($nomesImagens)) {
                setcookie("nomesImagens", $_FILES['upl']['name'], time()+300, "/");
            }
            else{
                $novoNome = $_COOKIE['nomesImagens'].",".$nomesImagens;
                setcookie("nomesImagens", $novoNome, time()+300, "/");
            }


            $tmpImagens = (isset($_COOKIE['tmpImagens'])) ? $_COOKIE['tmpImagens'] : '';

            if (empty($tmpImagens)) {
                setcookie("tmpImagens", $_FILES['upl']['tmp_name'], time()+300, "/");
            }
            else{
                $novoNome = $_COOKIE['tmpImagens'].",".$tmpImagens;
                setcookie("tmpImagens", $novoNome, time()+300, "/");
            }


        }

    }

    echo '{"status":"error"}';
    exit;

With each selected image it preloads the image and stores the tmp_name and the name of the image in cookies through an ajax request, but when inserting the whole form, the input files disappear so I have to enter by the cookies I have already recorded, it inserts into the database but does not allocate the images, it is possible to do this with temporary files?

  • Not that I know of. You can upload to a temporary folder. Then you take the image and delete everything.

  • Do you have any idea how it can be done ?

  • It depends on your application. Just upload the image in a temporary folder with the name of the image. Then you take it later when you upload. Make the copy of the image in the correct folder and delete from the temporary folder.

  • I’ll really have to do it, put it as an answer to get more organized

1 answer

2


A suggestion is to upload the image to a temporary folder with the image name.

When you are actually entering into the database, look in the temporary folder for the image as you have its name in the session or cookie. Make a copy to the correct folder and then delete the image from the temporary folder.

Laborious, but it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.