0

I've a form with too many fields. One of these fields is as follows:

<form action="go_to_preview.php" role="form" method="post" enctype="multipart/form-data">
  <input type="file" name="equip_image" id="equip_image">
</form>

Now the purpose of file go_to_preview.php is to show the data filled in by user in the form of labels. All the filled in data are also contained in hidden fields. There is also one Submit button present on this form. If user finds whatever he/she has filled in is perfect then user clicks the submit button present on file go_to_preview.php. As soon as user clicks on submit button I'll add the data I received through $_POST to the database. Now my issue is I'm not able to upload the file selected by user from the page containing file control. Along with $_POST(which is an array of data present into hidden fields) I'm sending the $_FILES array in a Session as follows :

//go_to_preview.php
session_start();
$_SESSION['FILES'] = $_FILES;

//final.php
session_start();
$files_var = $_SESSION['FILES'];
//use $files_var
unset($_SESSION['FILES']);

Till now everything goes fine. Now when I submit the form I've to upload the selected file to the server. For it I wrote following logic in final.php file :

move_uploaded_file( $_SESSION['FILES']['equip_image'], $target);
unset($_SESSION['FILES']);

The function gets executed properly but the file is not getting uploaded to the server. Can someone please help me in this regard? Thanks in advance. If you don't get my doubt clear you can also refer to my following question to get the clear idea of my issue: How to get the $_FILES array data as it is after form submission in following scenario?

6
  • @Flosculus:For getting the clear idea you can refer my question stackoverflow.com/questions/25752228/… Commented Sep 10, 2014 at 10:23
  • Sorry I deleted that comment because I forgot the file data wasn't actually stored in $_FILES. Despite that though, are you moving the uploaded file to somewhere permanent? Because it is only temporary for the duration of the request it was created. Commented Sep 10, 2014 at 10:25
  • 1
    To clarify, you are moving the image into the session, and then attempting to retrieve the image from the same session in a later request? The problem is that PHP sessions can be very ephemeral, so you'll suffer frequent data loss. If you want to do asynchronous uploading, you're better off using a queue - like PHPResque. If you can't do that, then at least use a naive implementation, where you move the image to a folder and treat the folder like a queue. Commented Sep 10, 2014 at 10:25
  • unset you session after uploading the files . i Think you are destroying your session before uploading . Commented Sep 10, 2014 at 10:26
  • @Flosculus:No, I'm not moving or storing any kind of $_FILES data apart from Session. Commented Sep 10, 2014 at 10:27

1 Answer 1

4

Based on your code sequence you are trying to store your uploaded files in session and later use them on final.php for processing or display . All files you upload goes to some temporary folder , and you are supposed to move it to some permanent storage before your connection closes . As the server is going to delete temporary file as soon as your connection is closed . So you are supposed to put your uploaded file to permanent space within your application e.g. uploads folder with write permission to Apache and then you can store link to your file in session.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 Im not sure the answer to the previous question accounted for this.
based on his question he wants user to fill the forms but doesn't wants to store data until the user go for final submit after review so he is looking for storing data in session which is not possible for files as i mentioned above.
Ideally you would have dedicated a few DB tables to a wizard progression system. Unless you have some kind of clean up the files may become orphaned when the session dies and the form isn't completed. However this should solve your missing files issue.
@KapilBhagchandani:As per your suggestion if I upload and save the file to the server in some folder when user first submits the form and then if I modify the $_FILES["file"]["tmp_name"] value to the location of image saved into folder previously and then again go for upload of file using $_FILES array. Will it work?
yes that can be treated as a kind of solution where you have to clean junk files by yourself and you just not need to upload again you just have to move or copy files from your upload folder . And i would like to recommend you to follow the way suggested by @Flosculus.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.