-2

I want my previously uploaded file to be automatically selected while I am editing details which also include file upload. I have used value attribute as in other types. But this doesn't work.

<input type="file" name="file" value="<?php echo $news['image']; ?> ">

I have to select the required file again while I am editing details. No file is selected as default while editing. I want to edit other details but not the file upload.

What can I do so that I do not have to select it again?

Ihor Vyspiansky
  • 918
  • 1
  • 6
  • 11
  • 1
    Does this answer your question? [Pre-Populate HTML form file input](https://stackoverflow.com/questions/16365668/pre-populate-html-form-file-input) – CBroe Aug 11 '20 at 08:17

2 Answers2

1

You can display image using

<img src="path/<?php echo $news['image']; ?>"> 

or if it is a file use the

<a href="path/<?php echo $news['my_file']; ?>">My File</a> 

tag to link.

When you update the DB make sure you update the field image only when the new file is selected.

Sajeewa
  • 74
  • 1
  • 13
0

"I have to select the required file again while I am editing details"

...no you don't. Just write the server-side code so that if no new file is selected, it doesn't delete the existing one when the form is submitted. And you can provide a link to the existing file on the edit form so that the user can see what's already there.


P.S. You can't make it select the same file again in the input box because

a) that's a security problem, your code cannot select files from the user's device - otherwise malicious websites could use hidden file inputs to secretly steal files from a device. The browser enforces that the user must manually select the file, in order to prevent that possibility

and

b) it wouldn't be logical to try and select the same file again anyway - remember that an input file takes files from the local device, not the server. But there's no guarantee that the file still exists in the same location (and your server won't know where that is anyway), and hasn't been moved, renamed or deleted, or that the form is even being used on the same device as previously. And even if you ignore all that, what would be the point of re-uploading a file you've already got on the server? It would just be a waste of bandwidth.

ADyson
  • 57,178
  • 14
  • 51
  • 63