leoarce
leoarce

Reputation: 549

How do i run php code depending if a file upload field has files selected or not?

I tried many different things and can't figure it out. None of these work:

if (isset($_FILES['files'])) { //doesn't work
if (!empty($_FILES['files'])) { //doesn't work
if (is_upload_file ($_FILES['files'])) { //doesn't work
if (isset($_FILES['files']) && count($_FILES['files']['error']) == 1 && $_FILES['files']['error'][0] > 0){ //doesn't work
if (file_exists($_FILES['files']['tmp_name']) || is_uploaded_file($_FILES['files']['tmp_name'])) { //works for no files, but not with files
if (file_exists($_FILES['files']) || is_uploaded_file($_FILES['files'])) { //works for no files, but not with files
if (file_exists($_FILES['files'])) { //works for no files, but not with files

My form has a field where one file or multiple files can be uploaded.

My form tag looks like this:

<form id="myform" class="form-style clearfix" action="" method="post" enctype="multipart/form-data">

My file upload input field looks like this:

<input type="file" multiple name="files[]" />

When posting, the code looks like this:

if (isset($_POST['submit'])) {

    if (isset($_FILES['files'])) { //looking for correct version of this line

        //do stuff if one or more files have been selected

    }

//do stuff with the submitted form, like creating variables and inserting into database

//header("Location: redirect-page.php");
echo "<meta http-equiv=\"refresh\" content=\"0;URL=redirect-page.php\">"; //using this version of redirect, because the above one doesn't work for some reason
exit;
}

The isset files section just doesn't want to work. I need it to run the code in between that section only if files were selected in that file upload field. If no files were selected, that section should be skipped. I can't make that happen.

Upvotes: 2

Views: 356

Answers (3)

mike1992
mike1992

Reputation: 23

     if ($_FILES['files']['size'][0] == 0) {
      // Do stuff as no file was selected for upload
    }

Hopefully this works for you

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 79024

$_FILES['files'] will always be an array with predefined subarrays each with a 0 index even if no file is uploaded:

if(!empty($_FILES['files']['name'][0])) {
    //do stuff
}

Or check the error:

if($_FILES['files']['error'][0]) == 0) {
    //do stuff
}
//or

if($_FILES['files']['error'][0]) != 0) {
    //don't do stuff we have an error
}

The result of print_r($_FILES); when no file is uploaded will be:

Array
(
    [files] => Array
        (
            [name] => Array
                (
                    [0] => 
                )

            [type] => Array
                (
                    [0] => 
                )

            [tmp_name] => Array
                (
                    [0] => 
                )

            [error] => Array
                (
                    [0] => 4
                )

            [size] => Array
                (
                    [0] => 0
                )

        )

)

Alternately you could check the size etc. Just know that there is always a $_FILES['files'] with subarrays each with a 0 index.

Upvotes: 3

Zemistr
Zemistr

Reputation: 1049

Try this solution ;)

<?php
if(isset($_POST['submit'])) {
    // array_filter = remove all files with empty name
    if(isset($_FILES['files']['name']) && array_filter($_FILES['files']['name'])) {

        //do stuff if one or more files have been selected

    }

    //do stuff with the submitted form, like creating variables and inserting into database

    //header("Location: redirect-page.php");
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=redirect-page.php\">"; //using this version of redirect, because the above one doesn't work for some reason
    exit;
}

Upvotes: 1

Related Questions