0

What I want:
I want to check if <input type="file"> is empty.

The problem:
I know it checks it. I actually think it is something with a cache or something, but can't find out where.
But when I do not select a file it also goes into the if...

This is what I do:

<?
  if (empty($_FILES['picture']['name']))
{

}
else
{
    //Does some file checks...
}
?>

This is the full page code:

<?
include 'core/init.php';

$id = $_POST['p_id'];
$title = $_POST['title'];
$content = $_POST['content'];
$u_id = uniqid();

if (empty($title))
{
    $error[] = "Titel mag niet leeg staan";
}

if (empty($content))
{
    $error[] = "Beschrijving mag niet leeg staan";
}

if (empty($_FILES['picture']['name']))
{

}
else
{
    $target_dir = "images/projects/";
    $file_name = $u_id . '.' . pathinfo($target_dir.$_FILES['picture']['name'],PATHINFO_EXTENSION);
    $target_file = $target_dir . $file_name;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST['submit']))
    {
        $check = getimagesize($_FILES["picture"]["tmp_name"]);
        if($check !== false)
        {
            $uploadOk = 1;
        }
        else
        {
            $error2[] = "Dit is geen geldig bestand";
            $uploadOk = 0;
        }
    }

    if (isset($_FILES['picture']) || $_FILES['picture']['error'] != UPLOAD_ERR_NO_FILE)
    {
        // Check if file already exists
        if (file_exists($target_file))
        {
            $error[] = "Bestandnaam bestaat al, geef het een andere naam";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["picture"]["size"] > 5000000)
        {
            $error[] = "Bestand is te groot";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif")
        {
            $error[] = "Wij accepteren alleen JPG, JPEG, PNG & GIF bestanden";
            $uploadOk = 0;
        }
    }
}

if (isset($error))
{
    foreach($error as $error)
    {
        echo "<div id='project_error_list'></div>" . $error;
    }
}
else
{
    if (empty($_FILES['picture']['name']))
    {

    }
    else
    {
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0)
        {
            $error2[] = "Uploaden van het bestand geanuleerd";
            // if everything is ok, try to upload file
        }
        else
        {
            if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file))
            {
                //Do the rest
            }
            else
            {
                $error2[] = "Er is iets misgegaan met het uploaden van het bestand";
            }
        }
    }

    if (isset($error2) && !empty($error2))
    {
        foreach($error2 as $error)
        {
            echo "<div id='project_error_list'></div>" . $error;
        }
    }
    else
    {
        echo edit_project($id, $title, $content, $file_name);
    }
}
?>

<form class="form-horizontal" id="project_edit_form" enctype="multipart/form-data">
    <div class="form-group">
        <label for="picture" class="control-label">Thumbnail</label>
        <input type="file" name="picture" class="form-control">
        <label class="control-label">Huidige Thumbnail</label>
        <img class="form-control" style="max-width:35%; height:auto;" src="images/projects/<?=(empty($data['picture'])) ? 'default.png' : $data['picture']?>">
        <input type="hidden" name="p_id" value="<?=$id?>">
        <input type="submit" id="save_project" value="Opslaan" class="btn btn-lg btn-primary" style="float:right;">
    </div>
</form>

Hope you people have enough info to help me. If not, please ask me.

Yaron Schwimmer
  • 5,327
  • 5
  • 36
  • 59

1 Answers1

0

Found the solution!

At the bottom of my full php code where I check $errro2 is set I needed to put this if/else in the part where I check if move_upload_file

This is what it should be now:

<?
if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file))
{
    //Do the rest

    if (isset($error2) && !empty($error2))
    {
        foreach($error2 as $error)
        {
            echo "<div id='project_error_list'></div>" . $error;
        }
    }
    else
    {
        echo edit_project($id, $title, $content, $file_name);
    }
}
else
{
    $error2[] = "Er is iets misgegaan met het uploaden van het bestand";
}