Welcome aboard! We are happy you are here and wish you good net-raft!
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
} 
this is all code:
<!DOCTYPE html>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id ="upl1" method="POST" enctype="multipart/form-data">
<input type="file" id="html_btn1" name="html_btn1"></p>
<button type="submit" form="upl1" value="Submit">Submit</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$target_dir = "Images/";
$target_file = $target_dir . basename($_FILES["html_btn1"]["name"]);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
}
}
?>
</body>
</html>
+ delete image
<!DOCTYPE html>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id ="upl1" method="POST" enctype="multipart/form-data">
<input type="file" id="html_btn1" name="html_btn1"></p>
<button type="submit" form="upl1" value="Submit">Submit</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$target_dir = "Images/";
$target_file = $target_dir . basename($_FILES["html_btn1"]["name"]);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
if (!unlink($target_file))
{
echo ("</br>Error deleting $target_file");
}
else
{
echo ("</br>Deleted $target_file");
}
}
else
{
echo "Your file doesn't exist.";
}
}
?>
</body>
</html>
The most helpful PHP solutions
detect browser phpPHP Click to see more ... 4.6K 408