dskld5423
dskld5423
2014-09-10 10:21
浏览 57
已采纳

为什么我无法使用PHP中的Session上传文件?

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyin2390
    dongyin2390 2014-09-10 10:30
    已采纳

    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.

    点赞 评论

相关推荐