表单提交后/表单提交错误后填写文件输入 [英] Fill file input after form submit / on form submit error

查看:60
本文介绍了表单提交后/表单提交错误后填写文件输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含默认输入(文本,选择等)和文件上传(< input type = file> )混合的多部分表单)。



我将表单验证类和Codeigniter的上传库结合使用以进行表单提交。效果很好。



对于我尚未找到解决方案的问题,我只有一个问题:如果用户选择了图像但错过了填写另一个必填字段(例如名称),然后表单验证类将阻止请求并向客户显示错误消息。



但是现在我遇到了问题,图像是已经成功提交,我不想让用户再次添加文件。因此,我想用这些数据预填充文件输入。



我尝试了其他类似的操作:

 <输入类型=文件 name =图像 value =<?php echo set_value('image','');?> /> 

,还花了一些时间在网上找到解决方案,但没有成功。

解决方案

在服务器端,您没有获得有关文件位于客户端计算机上的 where 的任何信息,因此在在用户成功上传图像但用户没有正确填写其余字段的情况下,您只需忽略 input type = file 字段完全保留文件,但保留服务器上文件的位置。有几种方法可以解决此问题,但这都涉及获取上传文件的绝对位置,并且:


  1. 将其作为文件重新插入使用的隐藏值< input type = hidden name = uploadedFile value =<?php echo $ absPath;?> /> ,然后检查是否存在 $ _ POST [‘uploadedFile’] 并适当地利用它。但这不是一个好主意,因为您现在将服务器路径暴露给最终用户(使自己容易受到恶意攻击。)

  2. 开始会话并在其中保存绝对路径 $ _ SESSION 变量,同时以重新尝试的形式向用户显示简单令牌。

我会坚持使用方法2,因此假设您已完成验证表单并上传文件的所有工作,并且文件位于 $ absFilePath ,您可以执行以下操作:

  session_start(); //这必须在您的PHP文件的最顶端

// ...

$ formToken = md5(time());
$ _SESSION [’uploadedFile’] [$ formToken] = $ absFilePath;

然后使用以下命令将令牌呈现为隐藏变量:

  if(!empty($ _ SESSION ['uploadedFile'] [$ formToken]))
echo'< input type = hidden name = formToken value ='。$ formToken。' />';

并使用



<$隐藏文件上传部分p $ p> if(empty($ _ SESSION ['uploadedFile'] [$ formToken]))
echo //< input type = file输出此处...

最后在表单提交代码中检查 formToken 的值,然后尝试使用 isset($ _ POST ['formToken']) $ _ FILES ['image'] c>,并使用以下方法处理它:

  $ absFilePath = $ _SESSION ['uploadedFile'] [$ _ POST ['formToken'] ]; 

Bam!现在,您拥有了绝对的文件路径,就好像文件像以前一样已被上传。



由于您没有提供足够的代码,因此我只能给您足够的说明来让您开始,但这应该绰绰有余。


I´ve a multipart form with a mixture of default inputs (text, select etc.) and a file upload (<input type="file">).

I´m using a combination of form validation class and upload library of Codeigniter for form submission. That works great.

I´ve only one problem for what I haven´t found a solution yet: If the user selects an image but misses to fill another required field (like "name"), then the form validation class blocks the request and shows an error message to the customer.

But now I´ve the problem, that the image was already submitted successfully and I don´t want to let the user add the file again. So I want to pre-fill the file input with this data.

I´ve tried different things like:

<input type="file" name="image" value="<?php echo set_value('image','');?>" />

and also spent time on finding a solution on the web but without success.

解决方案

On the server side, you do not get any information about where the file is located on the client's computer, so in the scenario of a user uploading an image successfully but the user hasn't filled out the rest of the fields properly, you have to simply omit the input type="file" field entirely but keep a store of where the file is located on your server. There's a few ways to go about this, but it all involves taking the absolute location of the uploaded file and:

  1. Inserting it back as a hidden value using <input type="hidden" name="uploadedFile" value="<?php echo $absPath; ?>" /> then checking for the existence of $_POST['uploadedFile'] and utilizing it appropriately. But this isn't a solid idea as you're now exposing server paths to the end-user (opens yourself up to malicious attack.)
  2. Starting a session and saving the absolute path in the $_SESSION variable while presenting the user with a simple token in their re-attempt form.

I'd stick with method 2, so assuming you've done all the work to validate the form and upload the file and your file is located in $absFilePath, you could do the following:

session_start(); // This needs to be at the very top of you PHP file

// ...

$formToken = md5(time());
$_SESSION['uploadedFile'][$formToken] = $absFilePath;

Then render the token as a hidden variable using:

if (!empty($_SESSION['uploadedFile'][$formToken]))
    echo '<input type="hidden" name="formToken" value="'.$formToken.'" />';

and hide the file upload portion using

if (empty($_SESSION['uploadedFile'][$formToken]))
    echo // <input type="file" output here...

finally inside of your form submission code check for the existence of a formToken value before attempting to load $_FILES['image'] using isset($_POST['formToken']), and handle it using:

$absFilePath = $_SESSION['uploadedFile'][$_POST['formToken']];

Bam! Now you have your absolute file path as if the file had been uploaded just like before.

Since you haven't given enough code, I can only given you enough instruction to get you started, but this should be more than enough.

这篇关于表单提交后/表单提交错误后填写文件输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆