未定义的索引和函数没有运行


undefined index and function doesn't run

我有这样的代码来启动一个脚本发送订单。

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();
}

如果有文件上传,我得到文件-1和文件-2的未定义索引的错误。如果没有上传文件,我也会得到同样的错误,但代码工作并根据需要注入订单。

我的HTML是这样的:

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

HTML中的submit是这样的:

      <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>

如果没有文件,我不希望它停止进程。

表单HTML代码:

<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>

我把名字改成了file-1和file-2。我添加了数组位置[0],但仍然得到:

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);" />

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

您的表单缺少"file"类型的输入。输入应该像这样:

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

如果没有这些输入,PHP将不会设置$_FILES数组,从而得到您所拥有的错误。

更新:

现在我看到了所有的表单,我注意到你的文件输入是这样的:
<input type="file" id="file-1" name="file-1-upload" />
<input type="file" id="file-2" name="file-2-upload" />
这意味着您需要通过PHP使用name属性访问这些内容,如下所示:
 $_FILES['file-1-upload']['tmp_name']
 $_FILES['file-2-upload']['tmp_name']