-1
<form action = "<?php $_SERVER["PHP_SELF"];?>" class="register-form" method="post" name="form1" encype="multipart/form-data">
   <input type="text" placeholder="Username" name="username" maxlength="15" required/>
   <input type="text" placeholder="Email Address" name="email" maxlength="30" required/>
   <input type="password" placeholder="Password" name="password" maxlength="15" required/>
   <input type="password" placeholder="Confirm Password" name="conf_password" maxlength="20" required/>
   <input type="file" name="file" accept="image/*"/>
   <button href="../index.html">create</button>
</form>

These are my HTML tags. I want to put a default image for this field. The name of the image is default-avatar.jpeg
I have written this code:

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 $file variable gets another value, when the user doesn't choose a 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?

Ani Naslyan
  • 134
  • 2
  • 15
  • [You cannot](http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html). Side note: whatever you want to accomplish, `addslashes()` is the wrong tool. – Álvaro González May 07 '17 at 13:52

2 Answers2

1

Files are stored in $_FILES, not in $_POST. So the data must be sent in $_FILES. And empty() function will help in this case.

$temp = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];

define("UPLOAD_DIR", "../images/users/");

if (isset($_FILES['file']) && !empty($_FILES["file"])) {
    $file = $_FILES["file"]["name"];

    $uploadfile = UPLOAD_DIR . $file;//implode()

    if (!$error>0) {
        move_uploaded_file($temp, $uploadfile);
        echo "<p>Success<p>";
    }
    else{
        $file = "default-avatar.jpeg";
        echo "<p>An error occurred.</p>";
        exit;
    }
Ani Naslyan
  • 134
  • 2
  • 15
0

Try this:

if (isset($_POST["file"]))
{
    if($_POST["file"] == "")
    {
        $file = "default-avatar.jpeg";
        echo "No<br>";
    }
    else
    {
        $file = addslashes($_POST["file"]);
        echo "Yes<br>";  
    }
}
tyrone 1988
  • 321
  • 2
  • 11
  • The problem is that this `$_POST["file"] == ""` doesn't work, because the value of the file field is never empty – Ani Naslyan May 07 '17 at 13:59
  • @Ani, ive just seen the comment you posted on the other answer. Because you have set the input type as `file`, you may want to view this: http://stackoverflow.com/questions/37182801/does-input-type-files-appear-in-post – tyrone 1988 May 07 '17 at 14:09
  • 1
    Yes, because Files are stored in `$_FILES`, not `$_POST`. Thanks for this link. – Ani Naslyan May 07 '17 at 14:17