Search code examples
phpundefined-index

undefined index and function doesn't run


I have code that looks like this to start a script to send order.

include('../../auto/util.php');
if (!isValidFileName($_FILES['file-1']['tmp_name']) | !isValidFileName($_FILES['file-2']['tmp_name']))
{
    echo "Invalid filename, <a href='index.php'>try again</a>.";
}
else if (count($_FILES) === 0)
{
    echo "Your files failed to upload, likely because together they exceeded 15MB. Please submit the order manually to <a href=\"mailto:abc123@abc123.com\">abc123@abc123.com</a>";
}
else
{
    newOrder_broker();
}

I am getting an error for undefined index of file-1 and file-2 if there's files uploaded. I get the same error if no files are uploaded but the code works and injects the order as needed.

My HTML looks like this:

<form id="docContainer" enctype="multipart/form-data" method="POST" action="newOrder_broker.php"

And the submit in the HTML looks like this:

      <div id="fb-submit-button-div" class="fb-item-alignment-left">
    <button class="fb-button-special" id="fb-submit-button" style="background-image: url(theme/default/images/btn_submit.png);" onclick="checkShipTo();">Submit</button>
  </div>

If there are no files, I don't want that to stop the process.

Form HTML code:

<body onload="checkCustoms()">
<!-- Start of the body content for CoffeeCup Web Form Builder -->

<?php  include("alert.php"); ?>

<form id="docContainer" enctype="multipart/form-data" method="POST" action="newOrder_broker.php"
class="fb-100-item-column fb-toplabel selected-object" style="" data-form="manual_iframe">

<!-- // lots of form fields here here -->

  <br />

            <div id="item17" class="fb-item fb-50-item-column" style="opacity: 1; ">
            <div class="fb-grouplabel">
              <label id="item17_label_0" style="display: inline; ">
                Upload front (Maximum size: 15MB)
              </label>
              <input type="checkbox" class="saveCheck" id="file-1-check" />
            </div>
            <div class="fb-button">
              <input type="file" id="file-1" data-hint="" name="file-1-upload" onchange="readURL(this);" />
            </div>
            <br />
            <img id="file-1-upload" src="#" alt="Preview (front)" width="200"/>
          </div>



          <div id="item31" class="fb-item fb-50-item-column" style="opacity: 1; ">
            <div class="fb-grouplabel">
              <label id="item31_label_0" style="display: inline; ">
                Upload back (Maximum size: 15MB)
              </label>
              <input type="checkbox" class="saveCheck" id="file-2-check" />
            </div>
            <div class="fb-button">
              <input type="file" id="file-2" data-hint="" name="file-2-upload" onchange="readURL(this);" />
            </div>
            <br />
            <img id="file-2-upload" src="#" alt="Preview (back)" width="200" />
          </div>


  <div id="fb-submit-button-div" class="fb-item-alignment-left">
    <button class="fb-button-special" id="fb-submit-button" style="background-image: url(theme/default/images/btn_submit.png);" onclick="checkShipTo();">Submit</button>
  </div>
  <button type="reset" onclick="localStorage.clear(); location.reload();">Reset</button>
  <input type="hidden" name="fb_form_custom_html" />
  <input type="hidden" name="fb_form_embedded" />
</form>

Ok so I changed name to "file-1" and "file-2". I added the array position [0] as well but still getting:

Notice: Undefined index: file-1 in /home/prima2go/public_html/broker/sdp/newOrder_broker.php on line 6

    Notice: Undefined index: file-2 in /home/prima2go/public_html/broker/sdp/newOrder_broker.php on line 6
    Your files failed to upload, likely because together they exceeded 15MB. Please submit the order manually to abc123@primaatlanta.com

    <input type="file" id="file-1" data-hint="" name="file-1" onchange="readURL(this);" />

a

if (!isValidFileName($_FILES['file-1'][0]['tmp_name']) | !isValidFileName($_FILES['file-2'][0]['tmp_name']))

Solution

  • Your form is missing inputs with type "file". The input should look something like this:

    <input type="file" value="">
    

    Without these inputs, your PHP will not set the $_FILES array, thus getting the errors you're having.

    Update:

    Now that I see all your form, I notice that your file inputs are like this:

    <input type="file" id="file-1" name="file-1-upload" />
    <input type="file" id="file-2" name="file-2-upload" />
    

    Which means that you need to access these through PHP using the name attribute, like this:

     $_FILES['file-1-upload']['tmp_name']
     $_FILES['file-2-upload']['tmp_name']