+ 3

set default image for input filed with php

This is the part of my HTML tags <input type="file" name="file" accept="image/*"/><br><br> I want to put a default image for this field. The name of the image is default-avatar.jpeg I have written this code:<br> if (isset($_POST["file"])){ $file = addslashes($_POST["file"]); echo "Yes<br>"; } elseif(empty($_POST['file'])){ $file = "default-avatar.jpeg"; echo "No<br>"; } So I suggested when the user chooses another file, the value of the $file variable gets another value, when the user doesn't choose file, the value of $file stays default-avatar.jpeg But the value of the file field is never empty. When I tried to echo it has always some value. If the user chooses another file, the value of $file variable is that file, but when he doesn't the value is nothing (I thought it is empty). So the isset($_POST["file"]) is always True. How can I solve this?

7th May 2017, 1:51 PM
Ani Naslyan
Ani Naslyan - avatar
2 Respostas
+ 6
Do this- $image=$_POST['file']; if(empty($image)){ $image="default-avatar.jpeg"; } I tried, it is working perfectly 😉
7th May 2017, 2:49 PM
Sachin Artani
Sachin Artani - avatar
+ 1
@Sachin Artani This code doesn't work properly, because the value of the image field is never empty. So the value of the image variable is always "default-avatar.jpeg". Can you give me a link in which you have referred this code. Thank you.
8th May 2017, 12:04 PM
Ani Naslyan
Ani Naslyan - avatar