如何修复文件已附加或未附加到客户端

时间:2015-10-24 06:23:45

标签: php post file-upload

我在文件上传按钮上苦苦挣扎,当我独立实现它时,它在其中正常工作。 但是当我在主表单中复制相同的代码时,它不起作用。

i.e. $_FILES['file']['name']

没有给我任何输出。 事实上我得到了

Notice: Undefined index: error message.

我试图检查isset($_FILES[file]是否有效。这表示文件尚未设置。

从这个我想我可以说在客户端本身我的文件没有被提取。

form.php的

<div class="col-lg-3 col-md-push-1">
                <div class="form-group">
                    <label for="InputName">Upload Photograph: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <input type="file" name="stud_photo" id="stud_photo" />
                                </span>
                            </span>

                    </div>
                </div>
            </div>




            <div class="clearfix"></div>
            <div class="col-lg-3 col-md-push-4">
                <div class="form-group">
                    <input type="submit" name="register"  id="register" class="btn btn-default btn-lg btn-block" value="Submit"> 
                </div>
            </div>

conn.php

    if (isset($_POST['register'])) {
            # code...

        $student_name = $_POST['surname'] . " " . $_POST['middlename'] . " " . $_POST['firstname'];
        $student_name = str_replace("'", "''", $student_name);
        $department_id = $_POST['department_id'];

        $university_reg_no = $_POST['university_reg_no'];

        $address = str_replace("'", "''", $_POST['address']);
        $state = $_POST['state'];
        $city = $_POST['city'];
        $zip = $_POST['zip'];
        $distance = $_POST['distance'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $Blood_group = $_POST['Blood_group'];


        $stud_photo = $_FILES['stud_photo']['name'];
        $imageFileType = pathinfo($stud_photo,PATHINFO_EXTENSION);
        $stud_photo = $university_reg_no.".".$imageFileType;
}

注意:我正在使用PHP和ORACLE。但我不认为这会影响到这一点。

2 个答案:

答案 0 :(得分:0)

for Files需要将此属性添加到表单标记中。

location /{
        error_page 404 =  @foobar;
}

答案 1 :(得分:0)

试试这段代码。在这里,我测试所有输入元素添加完美的工作。
              

  <title>file upload sample</title>


  <?php
  if (isset($_POST['register'])) {
            # code...

        $student_name = $_POST['surname'] . " " . $_POST['middlename'] . " " . $_POST['firstname'];
        $student_name = str_replace("'", "''", $student_name);
        $department_id = $_POST['department_id'];

        $university_reg_no = $_POST['university_reg_no'];

        $address = str_replace("'", "''", $_POST['address']);
        $state = $_POST['state'];
        $city = $_POST['city'];
        $zip = $_POST['zip'];
        $distance = $_POST['distance'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $Blood_group = $_POST['Blood_group'];


        $stud_photo = $_FILES['stud_photo']['name'];
        $imageFileType = pathinfo($stud_photo,PATHINFO_EXTENSION);
        $stud_photo = $university_reg_no.".".$imageFileType;
}

  ?>

  <!-- <script src="//code.jquery.com/jquery-1.9.0.min.js"></script> -->





</head>

<body>
<form method="post" enctype="multipart/form-data">
  <div class="col-lg-3 col-md-push-1">
                <div class="form-group">
                    <label for="InputName">Surname: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <input type="text" name="surname" id="surname" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Middle name: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <input type="text" name="middlename" id="middlename" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">First name: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <input type="text" name="firstname" id="firstname" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Department Id: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <input type="text" name="department_id" id="department_id" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Univercity reg no: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <input type="text" name="university_reg_no" id="university_reg_no" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Address: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <textarea name="address"></textarea>
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">State: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                   <input type="text" name="state" id="state" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">City: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                   <input type="text" name="city" id="city" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Zip: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                   <input type="text" name="zip" id="zip" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Email: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                   <input type="text" name="email" id="email" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Distance: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                   <input type="text" name="distance" id="distance" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Phone: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                   <input type="text" name="phone" id="phone" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Blood Group: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                   <input type="text" name="Blood_group" id="Blood_group" />
                                </span>
                            </span>

                    </div>
                </div>
                <div class="form-group">
                    <label for="InputName">Upload Photograph: </label>
                    <div class="input-group">
                            <span class="input-group-btn">
                                <span class="btn btn-default btn-file">
                                    <input type="file" name="stud_photo" id="stud_photo" />
                                </span>
                            </span>

                    </div>
                </div>
            </div>

            <div class="clearfix"></div>
            <div class="col-lg-3 col-md-push-4">
                <div class="form-group">
                    <input type="submit" name="register"  id="register" class="btn btn-default btn-lg btn-block" value="Submit"> 
                </div>
            </div>
            </form>
</body>

</html>

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

    <form method="post" enctype="multipart/form-data">
      <div class="col-lg-3 col-md-push-1">
                    <div class="form-group">
                        <label for="InputName">Surname: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                        <input type="text" name="surname" id="surname" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Middle name: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                        <input type="text" name="middlename" id="middlename" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">First name: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                        <input type="text" name="firstname" id="firstname" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Department Id: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                        <input type="text" name="department_id" id="department_id" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Univercity reg no: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                        <input type="text" name="university_reg_no" id="university_reg_no" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Address: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                        <textarea name="address"></textarea>
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">State: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                       <input type="text" name="state" id="state" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">City: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                       <input type="text" name="city" id="city" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Zip: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                       <input type="text" name="zip" id="zip" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Email: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                       <input type="text" name="email" id="email" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Distance: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                       <input type="text" name="distance" id="distance" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Phone: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                       <input type="text" name="phone" id="phone" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Blood Group: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                       <input type="text" name="Blood_group" id="Blood_group" />
                                    </span>
                                </span>

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="InputName">Upload Photograph: </label>
                        <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-default btn-file">
                                        <input type="file" name="stud_photo" id="stud_photo" />
                                    </span>
                                </span>

                        </div>
                    </div>
                </div>

                <div class="clearfix"></div>
                <div class="col-lg-3 col-md-push-4">
                    <div class="form-group">
                        <input type="submit" name="register"  id="register" class="btn btn-default btn-lg btn-block" value="Submit"> 
                    </div>
                </div>
                </form>
      <?php
      if (isset($_POST['register'])) {
                # code...

            $student_name = $_POST['surname'] . " " . $_POST['middlename'] . " " . $_POST['firstname'];
            $student_name = str_replace("'", "''", $student_name);
            $department_id = $_POST['department_id'];

            $university_reg_no = $_POST['university_reg_no'];

            $address = str_replace("'", "''", $_POST['address']);
            $state = $_POST['state'];
            $city = $_POST['city'];
            $zip = $_POST['zip'];
            $distance = $_POST['distance'];
            $phone = $_POST['phone'];
            $email = $_POST['email'];
            $Blood_group = $_POST['Blood_group'];


            $stud_photo = $_FILES['stud_photo']['name'];
            $imageFileType = pathinfo($stud_photo,PATHINFO_EXTENSION);
            $stud_photo = $university_reg_no.".".$imageFileType;
    }

      ?>