How to Assign a Local File Path to a File Input in HTML
The HTML element is used to allow users to select a file from their local computer to be uploaded to a server. However, due to security concerns, you cannot directly set the value of this input field to a client-side disk file system path.
Imagine if this were possible and a malicious website could exploit it with the following code:
<form name="foo" method="post" enctype="multipart/form-data"> <input type="file" value="c:/passwords.txt"> </form> <script>document.foo.submit();</script>
This would allow websites to gain unauthorized access to sensitive files, such as passwords, stored on your local computer.
Instead, browsers restrict the value of a file input field to publicly accessible web resources. While you can set the value to a remote file, this does not provide the same level of functionality as accessing a local file path.
The above is the detailed content of Can I Directly Set a Local File Path to an HTML File Input Element?. For more information, please follow other related articles on the PHP Chinese website!