Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mkfizi-29935843's avatar

Best practice to store temporary files?

I have a create product page where user can upload multiple images to the product. If validation error occurs when submitting the form, it will redirect back to create product page where the form are populated with old() values. Additionally, previously uploaded image files during submission will still be in the form after redirection so user will not have to re-upload the image files. Currently I have 2 approaches to handle this:

Approach 1: Base64

In this approach, I convert uploaded image files to Base64 format during data validation and store the value in session. When the page is redirected it will pickup session value and convert Base64 to File Blob and attached it to image file input.

Approach 1 Pros:

  • Maintain file data accross redirect without storing file in storage.
  • Flexibility to handle file in frontend without refering to DB.

Approach 1 Cons:

  • Increased session memory passed from server side to frontend.
  • Security concern as file data is shown in frontend as Base64 format.
  • File input manipulation when converting Base64 to File Blob is not guarenteed to simulate uploading file action properly. Currently when redirect occurs, adding dd($request->file) in post method will show the file have sizes. However upon refreshing the file will now show size is 0.

Approach 2: Temporary File Storage

In this approach, files are stored temporarily in storage when form is submitted. Image path in storage is then stored in session. Upon redirection, when resubmit the form without any validation error, it will pickup previously stored image and attach it to newly created product data.

Approach 2 Pros:

  • Simplicity in file handling without frontend manipulation.
  • Efficient memory handling and scalable for larger file size.

Approach 2 Cons:

  • Increased storage usage for storing temporary files.
  • Require file cleanup for unused temporary files.

Currently I am using Base64 approach to handle this. But since I'm having minor problem stated in Approach 1 Cons point no 3, I'm starting to lean towards Approach 2. However, the fact that I have to do file cleanup for unused temporary file bugs me. Obviously this wouldn't be a problem if the app simply ask user to reupload the file again upon redirection. But I personally think that would be a bad user experience.

That being said, what are the best practice that is used in industry?

0 likes
2 replies
Tray2's avatar

You really shouldn't reuse the uploaded images, just set the files paths to the old value, and upload them again, or validate your data before uploading the files.

Snapey's avatar
  1. NO, this is impractical

  2. Yes, put the files in temporary storage. If later the product is not added, you can clean up the temp files. Avoid problems in the first place by using client side validation (as well as server side)

Alternatively alter the flow to create the product first then add the photos as a second step. Think about it this way, suppose the product already exists and the user needs to update the photos? Often I tackle the edit situation first and then the create function is easy

Please or to participate in this conversation.