Validating empty $_FILES

OrdepTheWizard

I have a simple register form where the user can upload a profile picture, if the user doesn't it, it should take the default picture name called person-icon.png.

When I register an user and upload a picture it works but if i leave it blank don't do anything and that column is inserted into the DB empty

if(isset($_FILES['image'])){
  $img = $_FILES['image']['name'];
}
else if(empty($_FILES['image']['name'])){
  $img = 'person-icon.png';
}

I already have tried these options:

Option 1:

if (empty($_FILES['image'])){
  $img = 'person-icon.png';
}
else{
  $img = $_FILES['image']['name'];
}

Option 2:

if($_FILES["image"]["error"] == 4)  

Option 3:

if($_FILES["image"]["name"] == "")
LFS96

You could check for $_FILE['image']['name'] not equal "" this work; See code:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="image" id="image">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html> 
<?php

//var_dump($_FILES);
$default_pic =" pic.png";
if (isset($_FILES['image'])){
    if($_FILES['image']['name'] != ""){
        echo "has pic";
    }
    else{
        $_FILES['image']['name'] = $default_pic;
        echo "has no pic";
    }

}
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP - $_FILES array is empty

From Dev

Eclipse made files empty

From Dev

$_FILES is always empty

From Dev

PHP session files are empty

From Dev

Validating a ListBox's contents are not empty

From Dev

CakePHP $_FILES empty

From Dev

Rsync - not transferring empty files

From Dev

Validating Null and Empty Strings using Jersey

From Dev

Validating files in C

From Dev

Bulk validating yaml files

From Dev

Validating label content equal to null or string.Empty

From Dev

Validating a string for a positive number, null/empty string

From Dev

Validating TinyMCE for empty inputs

From Dev

Validating multiple files in array

From Dev

Validating files in Zend Framework 2

From Dev

Validating if a combobox it's SelectedText property is empty always fails

From Dev

Where are PEM files stored for validating SSL certificates?

From Dev

Validating a ListBox's contents are not empty

From Dev

How to disable validating empty fields with bootstrapvalidator?

From Dev

How to allow the empty string when validating an integer value

From Dev

Validating Null and Empty Strings using Jersey

From Dev

Bulk validating yaml files

From Dev

Validating TinyMCE for empty inputs

From Dev

Validating multiple files in array

From Dev

Create a list with empty files and a list with not empty files

From Dev

empty() check not validating after POST

From Dev

Is there a tool for validating /etc/network/interfaces files?

From Dev

Jquery validating not empty and is URL in forms

From Dev

Sequalize validating email, fails with Empty String