-1

On my Magento 2 site I created a form that adds products to the cart programmatically using a custom module. In my custom module I created the execute function for my form action in the product page. In this function I add the products to the cart and redirect the checkout. Other than that I am trying to transfer the files, which I upload to the product page, to the checkout page (which is the redirect of my function). In summary what I would like to do is:

  1. I send the form from the product page
  2. With the execute function, which is called in the form action, I want to get the files I loaded from the input file tag
  3. I transfer these uploaded files to the checkout page, which is the final redirect of my execute () function
  4. I would like the value of my input file to be stored, on the checkout page, on another input file tag

this is my code:

Page 1 (Product Page)
<form action="myFunctionExecute()" method="post">
<input type="file" id="file" name="file" />
<input type="submit" value="send" />
</form>

Page of function
public function myFunctionExecute(){
  $_SESSION['file'] = $_FILES['logoUp']['name'];

  return $resultRedirect->setPath('checkout');
}

Page 2 (Checkout Page)
//IN THIS INPUT I WANT TO TRANSFER AND KEEP VALUE OF INPUT FILE FROM PAGE 1
<input type="file" name="file"/>

Is it possible to transfer the value of an input file tag and take it to another input file tag of another page? Thanks!

Jackom
  • 384
  • 3
  • 16
  • What do you mean by "the value of my input file to be stored"? – Nico Haase Aug 26 '20 at 16:13
  • Maybe https://stackoverflow.com/questions/7664902/php-multi-step-form-with-file-upload helps? – Nico Haase Aug 26 '20 at 16:14
  • @NicoHaase In practice, every time I try to upload files to the product page and submit the form I would like these files to be stored on the checkout page (page 2) and kept until the order is completed. It's possible this? – Jackom Aug 26 '20 at 16:27
  • Yeah, that's possible - have a look at the link I've posted – Nico Haase Aug 26 '20 at 19:48
  • Ok man, thanks! Maybe i found a way! :) Another question please: After saved my file in a temp folder it's possible after sended a new form move this file from folder to another of my website? – Jackom Aug 27 '20 at 07:58
  • Yes, of course. You can move these files wherever you want – Nico Haase Aug 27 '20 at 08:00

1 Answers1

0

You can use $_SESSION variables. These are varibles that will stay active for as long as the user is on the website which will work on other pages aswell.

Page 1:

$_SESSION['page'] = 'page1';

Page 2:

echo $_SESSION['page'];

Will echo "page1"

NOTE: If you want to use session variables you need to initialize it by doing:

session_start();

on the beginning of every page you set/get session variables.

Timberman
  • 647
  • 8
  • 24
  • So with SESSION variable I can keep the value of the input file from one page to another every time I submit a form, is that correct? – Jackom Aug 26 '20 at 14:51
  • Ok, well! Please check my updated question. There is the code that I am trying. When I'm on page 2 how can I transfer the session variable to the new input file? – Jackom Aug 26 '20 at 16:10