[英]how do I change the text that is displayed for file input form, or “ input type='file' ”
Is there an attribute for this? 有这个属性吗? Because if there is I can't find it.
因为如果找不到我。
What I want to do is just echo out the file name that was selected by the user after the form is submitted. 我想做的就是在提交表单后回显用户选择的文件名。 To be clear, it already shows the file name, but problem is that the form could get submitted in the wrong state, in which case, the yellow text goes back to "No file chosen."
为了清楚起见,它已经显示了文件名,但是问题在于表单可能以错误的状态提交,在这种情况下,黄色文本返回到“未选择文件”。 I just need the file name to persist after submitting the form.
提交表单后,我只需要保留文件名即可。
I have a form... 我有一个表格...
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
<?php
//upload files here.
$fcount = 0;
foreach ($layouts[$my_layout] as $key => $value) {
//create the form
echo 'choose an image with an aspect ratio of ' . $value;
?>
<input type="file" name="filesToUpload[]" >
</form>
on the page, I want to change the yellow text to the file name, when such a file is chosen, once the form is submitted. 在页面上,一旦提交表单,我想将黄色文本更改为文件名。
While your explicit question is asking how to change the text, the implied question is that you want to set the value of the input so the same file will be uploaded again. 当您的显式问题询问如何更改文本时,隐含的问题是您要设置输入的值,以便将同一文件再次上载。
You can't do that. 你不能那样做。 That would require allowing the page author to determine what file gets uploaded from the user's computer.
那将需要允许页面作者确定从用户计算机上载了什么文件。 This would be a serious security risk.
这将是严重的安全风险。
What you can do is store the uploaded file on the server (eg in a temp directory that has old files deleted on a regular basis, eg with cron), and put a reference to it in the new form. 您可以做的是将上载的文件存储在服务器上(例如,在具有定期删除旧文件的temp目录中,例如使用cron),然后以新格式对其进行引用。
<label> <input type="checkbox" name="already_uploaded_file" value="my_file_identifier" checked> Use foo.jpeg </label>
<label> Upload a different file <input type="file" name="foo"> </label>
Once you have the File, you should be able to get a originalName
property from it. 获得文件后,您应该可以从中获取
originalName
属性。 Try $fileName = $request->file('name')->getClientOriginalName();
尝试
$fileName = $request->file('name')->getClientOriginalName();
. 。
Note: this is untested and I am assuming that both the file()
method on the Request
object returns an UploadedFile
instance and that Laravel's UploadedFile
class is either extending Symfony's class or using it directly. 注意:这未经测试,我假设
Request
对象上的file()
方法都返回一个UploadedFile
实例,并且Laravel的UploadedFile
类正在扩展Symfony的类或直接使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.