未定义的索引和函数不运行

时间:2015-10-05 19:40:12

标签: php undefined-index

我的代码看起来像启动脚本来发送订单。

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中的提交如下所示:

      <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']))

1 个答案:

答案 0 :(得分:0)

您的表单缺少类型为“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" />

这意味着您需要使用name属性通过PHP访问这些内容,如下所示:

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