I am trying to process an upload from another page. Once I submit, I get the error alert to ''please select an .CSV file' so it doesn't seem that the form name and id is being addressed. I do have other multiple forms on the same page none of which have the same submit name. Do I need to be more specific with '$_FILES['file']['name']' in the results form?

The Form

<div id="myModal6" class="modal">
        <div class="modal-content-contribute">
            <div class="modal-header">
                <span class="close">&times;</span>
                <h2>Upload Multiple lessons in .CSV format</h2>
            </div>
            <div class="modal-body-contribute">

<div id="message1"></div>
      <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">Select CSV File</h3>
          </div>
          <div class="panel-body">
            <div class="row" id="upload_area">
              <form method="post" id="upload_form" enctype="multipart/form-data">
                <div class="col-md-6" align="right">Select File</div>
                <div class="col-md-6">
                  <input type="file" name="file" id="csv_file" />
                </div>
                <br /><br /><br />
                <div class="col-md-12" align="center">
                  <input type="submit" name="upload_file" id="upload_file" class="btn btn-primary" value="Upload" />
                </div>
              </form>
              
</div> <div class="table-responsive" id="process_area"> </div> </div> </div> </div> </div></div>';

Results Page

<?php

//upload.php

session_start();

$error = '';

$html = '';

if($_FILES['file']['name'] != '')
{
 $file_array = explode(".", $_FILES['file']['name']);

 $extension = end($file_array);

 if($extension == 'csv')
 {
  $file_data = fopen($_FILES['file']['tmp_name'], 'r');

  $file_header = fgetcsv($file_data);

  $html .= '<table class="table table-bordered"><tr>';

  for($count = 0; $count < count($file_header); $count++)
  {
   $html .= '
   <th>
    <select name="set_column_data" class="form-control set_column_data" data-column_number="'.$count.'">
     <option value="">Set Count Data</option>
     <option value="title">Title</option>
     <option value="description">Lesson Description</option>
     <option value="learningactivities">Activities</option>
     <option value="materials">Materials</option>
     <option value="healthobj">Health Objective</option>
     <option value="nutriobj">Nutrition Objective</option>
     <option value="opener">Opener</option>
     <option value="closer">Closer</option>
     <option value="adaptation">Adaptation</option>
     <option value="recipe">Recipe</option>
    </select>
   </th>
   ';
  }

  $html .= '</tr>';

  $limit = 0;

  while(($row = fgetcsv($file_data)) !== FALSE)
  {
   $limit++;

   if($limit < 6)
   {
    $html .= '<tr>';

for($count = 0; $count < count($row); $count++)
{
 $html .= '<td>'.$row[$count].'</td>';
}

$html .= '</tr>';
   }

   $temp_data[] = $row;
  }

  $_SESSION['file_data'] = $temp_data;

  $html .= '
  </table>
  <br />
  <div align="right">
   <button type="button" name="import" id="import" class="btn btn-success" disabled>Import</button>
  </div>
  <br />
  ';
 }
 else
 {
  $error = 'Only <b>.csv</b> file allowed';
 }
}
else
{
 $error = 'Please Select CSV File';
}

$output = array(
 'error'  => $error,
 'output' => $html
);

echo json_encode($output);


?>

    So how does your form know which page to submit to?

      You are apparently using ajax to submit the form, based on the form tag and the json output. Uploading a file using ajax requires some specific coding in the browser. Are you sure that anything is being uploaded. To log the $_FILES information, add the following line of code near the start of upload.php -

      file_put_contents('log.txt',print_r($_FILES,true),FILE_APPEND);
      

      Next, your form processing code is missing error detection and handling that would get your code to either work or to tell you why it isn't working. The form processing code should -

      1. Detect if a post method form was submitted before referencing any of the form data.
      2. If the total size of the form data exceeds the post_max_size setting, both the $_POST and $_FILES arrays will be empty. There are other things that can cause this, such as uploads not being enabled on the server or bad html markup on the web page, but once you have successfully uploaded a file at all, this is the reason that these will be empty. You need to detect this condition and setup a message for the user letting them know that the size of the form data was too large and could not be processed. You would also want to log information about this so that you know it is occurring on a live site.
      3. Once you have determined that there is data in $_FILES, you need to test the ['error'] element. If there was no upload error, you can then use and process the uploaded file information. For any of the upload errors that the user has control over, you need to setup a unique and helpful error message for each one letting the user know what they did wrong and how to correct the problem. For the errors that the user has no control over, you would setup and display a general failure message and log the information about the actual error so that you know it is occurring on a live site.
      4. Since your code is directly opening and reading the contents of the uploaded file, you should use is_uploaded_file() in a test to make sure that someone hasn't setup some fake data in the $_FILES array and has managed to get your code to execute and use that data. You would also want to have error detection and handling for the fopen() statement so that you are not trying to read data from a file that could not be opened.

        And to repeat a reply to one of your earlier posts:

        For further debugging you can actually look at the contents of the $_POST array to see what the server is receiving from the browser. Your browser will also have developer tools to allow seeing what the browser is sending to the server.

        Spending a couple of hours debugging can save a day of waiting for a reply. Even if you don't solve the problem yourself as a result, you'll be able to narrow down where the problem is and have some useful information you can put in your post.

          5 days later

          I had embedded the code in a separate iframe and it worked.

            This code is also can be helpful in my problem too, let's say it would be helpful for a moment I would really appreciate the efforts made in it , while i am looking forward to further solutions.

              21 days later

              I am a preschool teacher that has been virtual since the beginning of the year. I have had no difficulties uploading any videos until this week. None of my videos that I upload are processing. I have tried multiple computers. I have tried multiple browsers (and have cleared the cookies).
              Thanks.

              activenoon None of my videos that I upload are processing.

              Uploading to what? Is it your own PHP code? (This is, after all, a PHP forum.)

                Write a Reply...