-2
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="header">
        <H2>Login</H2>
    </div>

    <form method="post" action="https://..." enctype="multipart/form-data">
        <?php include 'errors.php'; ?>
        <div class="input-group">
            <input type="text" name="username" placeholder="Username..." value="<?php echo $username; ?>">
        </div>
        <div class="input-group">
            <input type="password" name="password_1" placeholder="Password...">
        </div>
        </div>
        <div class="input-group">
            <input type="file" name="image" placeholder="Image..." value="<?php echo $image; ?>"> // The File doesn't get echo in after submit and an error
        </div>
        <div class="input-group">
            <button type="submit" name="login" class="btn">Login</button>
        </div>
    </form>
</body>
</html>

This Form is to register Users with an Image. After submit I check if there Name is all ready taken and look for other errors if there is an error I want that everything is still in the Form except the password. It is working with the Username but not with a file (Image). So how can I do that?

  • It is not possible to pre-populate the file elements. It is built into the browsers this way. It is to prevent you setting a value to upload files from the users computer. The user needs to actually select a file to populate this. – mic Sep 11 '20 at 21:02
  • ok I see not even with ['tmp_name'];? –  Sep 11 '20 at 21:04
  • nope, unfortunately not. If you was able to set this value it would be a hackers dream ;) – mic Sep 11 '20 at 21:05
  • You are right. Thank you :) –  Sep 11 '20 at 21:07
  • Exact duplicate of [Pre-Populate HTML form file input](https://stackoverflow.com/questions/16365668/pre-populate-html-form-file-input) – MrUpsidown Oct 26 '20 at 19:30

2 Answers2

-1

It is by default impossible to set a default value for a file picker due to security reasons

Learn more about it here

Souhailhimself
  • 221
  • 1
  • 10
-2

Others have explained why you can't do it. But if it is very important for you to achieve this behaviour (retaining all elements if there is an error in submission details), you can do this by setting up Ajax calls. Since you would also add preventDefault() to the submit button event handler, the page won't be refreshed and the user won't lose entered data. Your first ajax call will verify if entered data is valid. And then, if the response is positive, you send another ajax request to process the form submission.

EmirDev
  • 87
  • 5