PHPLover
PHPLover

Reputation: 12957

Why I'm not able to upload file using Session in PHP?

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?

Upvotes: 0

Views: 1532

Answers (1)

Kapil Bhagchandani
Kapil Bhagchandani

Reputation: 493

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.

Upvotes: 4

Related Questions