php $_FILE variable undefined index

ghz 7months ago ⋅ 89 views
<?php

 if ($_SERVER['REQUEST_METHOD']== "POST") {
    if(isset($_POST['submit']){
            echo $_POST['vid'];   
}}

// This part of the code recieves data from the form on the $_POST variable which i dont want in the post variable but i need in the $_FILES variable

 if ($_SERVER['REQUEST_METHOD']== "POST") {
    if(isset($_POST['submit']){
            echo $_FILES['vid']['tmp_name'];   
}}

// but here i get the Undefined index in the $_FILES variable and is null ?>

 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="mutlipart/form-data">
    <div class="row">
      <div class="col-6">
        
        <div class="form-group">
        <label for="formGroupExampleInput" >عنوان الدرس </label>
        <input type="text" class="form-control" id="formGroupExampleInput" placeholder="عنوان الدرس" name="lsn">
        </div>
      </div>
      <div class="col-6">
        
          <div class="form-group">
          <label for="formGroupExampleInput">رقم الدرس  </label>
          <input type="text" class="form-control" id="formGroupExampleInput" placeholder="رقم الدرس " name="no">
          </div>
      </div>

    </div>
    <div class="row">
    <div class="col-6">
        <input type="file" id="uploadbtn" name="vid" value=""/> // this uploads video in to the file variable
        <label for="basic-url" class="form-labe4" >  ملخص الدرس  </label>
      <label class="lbtn" for="uploadbtn">
        <svg  xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi ii bi-upload" viewBox="0 0 16 16">
        <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5"/>
        <path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708z"/>
      </svg><span></span></label>
      </div>
      
      <div class="col-6">
        <input type="file" id="uploadbt" name="book" value=""> this file uploads a book of pdf
        <label for="basic-url" class="form-labe4" >  الكتاب الدرس  </label>
      <label class="lbtn" for="uploadbt">
        <svg  xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi ii bi-upload" viewBox="0 0 16 16">
        <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5"/>
        <path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708z"/>
      </svg><span></span></label>
      </div></div>
    
    
      <input class="bttn-n" type="submit" name="submit"> رفع الدرس // this is the submit button the uploads
                </input>
    </form>

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

This is the form HTML code

To be assure that its not a php problem i tried the same idea in a simple form in a different page without any css or javascript and it worked fine, What could be the problem in the html?

Answers

It seems like you have a couple of issues in your HTML form:

  1. In your submit button, you haven't specified a value for the submit input. Instead of <input class="bttn-n" type="submit" name="submit">, you should use <input class="bttn-n" type="submit" name="submit" value="Submit">.

  2. For file inputs, you should remove the value attribute, as it doesn't make sense to specify a default value for file inputs. So, change <input type="file" id="uploadbtn" name="vid" value=""/> to <input type="file" id="uploadbtn" name="vid"/>.

Here's the corrected HTML code:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="mutlipart/form-data">
    <div class="row">
        <div class="col-6">
            <div class="form-group">
                <label for="formGroupExampleInput">عنوان الدرس</label>
                <input type="text" class="form-control" id="formGroupExampleInput" placeholder="عنوان الدرس" name="lsn">
            </div>
        </div>
        <div class="col-6">
            <div class="form-group">
                <label for="formGroupExampleInput">رقم الدرس</label>
                <input type="text" class="form-control" id="formGroupExampleInput" placeholder="رقم الدرس" name="no">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-6">
            <input type="file" id="uploadbtn" name="vid"/>
            <label for="basic-url" class="form-labe4">ملخص الدرس</label>
            <label class="lbtn" for="uploadbtn">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi ii bi-upload" viewBox="0 0 16 16">
                    <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5"/>
                    <path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708z"/>
                </svg><span></span>
            </label>
        </div>
        <div class="col-6">
            <input type="file" id="uploadbt" name="book"/>
            <label for="basic-url" class="form-labe4">الكتاب الدرس</label>
            <label class="lbtn" for="uploadbt">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi ii bi-upload" viewBox="0 0 16 16">
                    <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5"/>
                    <path d="M7.646 1.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 2.707V11.5a.5.5 0 0 1-1 0V2.707L5.354 4.854a.5.5 0 1 1-.708-.708z"/>
                </svg><span></span>
            </label>
        </div>
    </div>
    <input class="bttn-n" type="submit" name="submit" value="Submit">رفع الدرس
</form>

With these changes, the form should submit the values of the file inputs correctly.