简体   繁体   中英

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.

 <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. Try $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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM