How to get the $_FILES array data as it is after form submission in following scenario?

旧街凉风 提交于 2019-12-25 07:10:13

问题


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. 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 along with $_POST(which is an array of data present into hidden fields) I want the data from $_FILES as it is. How should I achieve this? Can someone please help me in this regard? Thanks in advance.


回答1:


A common approach would be sessions:

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

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



回答2:


If I have understood your question correctly you are asking if you can pass both the $_FILES and $_POST arrays for processing in go_to_preview.php, the answer is yes.

The $_FILES array is populated when you selected an image via your equip_image element. Upon posting your form any specified POST data in that form will be sent in the headers to go_to_preview.php, the $_FILES array is sent too, this is seperate from your $_POST array.

In your go_to_preview.php you can then get the contents of both arrays:

$file = $_FILES['equip_image'];
$data = $_POST['form_data'];

If you wish to then unset that data you simply call

unset($_SESSION['FILES']);


来源:https://stackoverflow.com/questions/25752228/how-to-get-the-files-array-data-as-it-is-after-form-submission-in-following-sc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!