user8231918
user8231918

Reputation:

Storing FILE data in a session for later use move_uploaded_file

I dont know if this is possible but I need to store the FILE data into a session so I can in the future uses its information to store images into the server.

At the moment the drag and drop is storing data to the server straight way.

Basically I have a form where user can fill in information of the product and then add images. once user enters enters required information and press submit, those information will then be stored into mysql where a product ID is created. with that product key I would use it to name the images such as product_ID + product_namme + image number.jpg The problem is I cant find a way to store the FILE data up to this point, I have tried using session, but when I echo its value its blank.

PHP This version works but store the data straight way so i cant rename the file. This is happening before

foreach($_FILES['file']['name'] as $position => $name){
    if(move_uploaded_file($_FILES['file']['tmp_name'][$position], '../ProductImages/'.$name));
}

This is the code with session in it, however when I echo those sessions after user has pressed submited, they are blank. If I echo just below this code it will have data. Is there something Im missing?

if(!empty($_FILES['file']['name'][0])){
    $_SESSION['imgAmount'] = 1;
    foreach($_FILES['file']['name'] as $position){
        $_SESSION['tmpVal'][$position] = $_FILES['file']['tmp_name'][$position];
        $_SESSION['imgAmount']++;
    }
}

This is where the seesion is called

$i = 0;
 if(isset($_SESSION['tmpVal'])){
        while($i < $_SESSION['imgAmount']){
            move_uploaded_file($_SESSION['tmpVal'][$i],'../ProductImages/'.$imgID . 'child' . $i . '.jpg');
            $i++;
            echo "test0";
            echo "<br>" . $_SESSION ['tmpVal'];
            echo "<br>" . $_SESSION ['imgAmount'];
            echo "<br>" . $_SESSION ['tmpVal'][0];
            echo "<br>" . $_SESSION ['tmpVal'][1];

        }
    }

Upvotes: 4

Views: 2850

Answers (1)

Dhaval Chheda
Dhaval Chheda

Reputation: 5147

You cannot store files into sessions and use them later

I have implemented the below code in 1 of my projects in which I save the files to 1 location and then on a condition been met I copy them to the actual target location and delete from the temporary location

//Initial storing
for ($i = 0; $i < count($_FILES['files']['name']); $i++)
        {
            if ($_FILES['files']['error'][ $i ] == 0)
            {
                $tmpName = $_FILES['files']['tmp_name'][ $i ];
                $name = $_FILES['files']['name'][ $i ];
                $location = "temp/";

                move_uploaded_file($tmpName, $location . $name);
            }
        }
session(['filesList' => $_FILES]);

//Final moving to the actual target location

$fileList = session('filesList');
if (count($fileList['files']['name']) > 0)
        {
            for ($i = 0; $i < count($fileList['files']['name']); $i++)
            {
                if ($fileList['files']['error'][ $i ] == 0)
                {
                    $name = $fileList['files']['name'][ $i ];

                    $transferFile = "temp/" . $name;

                    $location[] = "files/" . $userId . $name;

                    copy($transferFile, $location[ $i ]);
                    unlink('temp/' . $name);
                }
            }
        }

so basically except the session code which is in laravel you can use the rest of the code in core PHP as well

Hope this helps

Upvotes: 3

Related Questions