<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>
這些是我的HTML標籤。 我想爲此字段設置一個默認圖像。圖像的名字是default-avatar.jpeg
我寫了這個代碼:
用php爲MySQL數據庫設置用戶的默認圖像
if (isset($_POST["file"])){
$file = addslashes($_POST["file"]);
echo "Yes<br>";
}
elseif(empty($_POST['file'])){
$file = "default-avatar.jpeg";
echo "No<br>";
}
所以我建議當用戶選擇另一個文件中,$file
變量得到另一個值,當用戶不選擇一個文件時, $file
停留的值default-avatar.jpeg
但是文件字段的值永遠不會爲空。當我試圖echo
它總是有一些價值。如果用戶選擇另一個文件,變量$file
的值是該文件,但是當它不是該值時(我認爲它是空的)。所以isset($_POST["file"])
總是爲真。我該如何解決這個問題?
[你不能](http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html)。附註:無論你想完成什麼,'addslashes()'都是錯誤的工具。 –