Notice: Undefined index: fileToUpload in C:\xampp\htdocs\database_upload\upload.php on line 9

<?php
include 'config.php';
$class="";
$message='';
$error=0;
$target_dir = dirname(__FILE__)."/Uploads/";
if(isset($_POST['btn-upload']))
{  
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
	$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
	if($fileType != "csv")  // here we are checking for the file extension. We are not allowing othre then (.csv) extension .
	{
		$message .= "Sorry, only CSV file is allowed.<br>";
		$error=1;
	}
	else
	{
		if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
			$message .="File uplaoded successfully.<br>";
			
			if (($getdata = fopen($target_file, "r")) !== FALSE) {
			   fgetcsv($getdata);   
			   while (($data = fgetcsv($getdata)) !== FALSE) {
					$fieldCount = count($data);
					for ($c=0; $c < $fieldCount; $c++) {
					  $columnData[$c] = $data[$c];
					}
             $name = mysqli_real_escape_string($connect ,$columnData[0]);
             $cpu = mysqli_real_escape_string($connect ,$columnData[1]);
			 $memory = mysqli_real_escape_string($connect ,$columnData[2]);
			 $disk = mysqli_real_escape_string($connect ,$columnData[3]);
			 $nic = mysqli_real_escape_string($connect ,$columnData[4]);
			 $price = mysqli_real_escape_string($connect ,$columnData[5]);
			 
             $import_data[]="('".$name."','".$cpu."','".$memory."','".$disk."','".$nic."','".$price."')";
            // SQL Query to insert data into DataBase

             }
             $import_data = implode(",", $import_data);
             $query = "INSERT INTO reportdec(name, cpu, memory, disk, nic, price) VALUES  $import_data ;";
             $result = mysqli_query($db ,$query);
			 $message .="Data imported successfully.";
			 fclose($getdata);
			}
				
		} else {
			$message .="Sorry, there was an error uploading your file.";
			$error=1;
		}
	}

$class="warning";
if($error!=1)
{
	$class="success";
}

}
 
?>

Open in new window


Line 9 -     $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
Dave ITnewbie ITAsked:
Who is Participating?
 
gr8gonzoConsultantCommented:
If the "name" attribute of your file input is "file", then the result will be in $_FILES["file"]. Or you can change the name of your input to match your code, like this:
<input type="file" multiple   name="fileToUpload"  />

Open in new window

1
 
hieloCommented:
Are you uploading the file via:
A.) ajax? Be sure to attach the file to a FormData object and pass the instance of the FormData object to the ajax send method
      -- see http://blog.teamtreehouse.com/uploading-files-ajax
B.) a traditional web form? If so, did you remember to add enctype to the form;
    <form enctype="multipart/form-data" method="post"...>...</form>
1
 
gr8gonzoConsultantCommented:
My money is on hielo's second option "B" - I've lost count of how many people forget to add the right encoding type to the form and can't figure out why the file isn't properly uploading.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Dave ITnewbie ITAuthor Commented:
A.) ajax? Be sure to attach the file to a FormData object and pass the instance of the FormData object to the ajax send method
PHP MYSQLI
0
 
Dave ITnewbie ITAuthor Commented:
 <form action="upload.php" method="post" enctype="multipart/form-data">
 <input type="file" multiple   name="file"  />
 <button type="submit" name="btn-upload" style="height: 25px; width: 100px">UPLOAD</button>
 </form>

Open in new window

0
 
Dave ITnewbie ITAuthor Commented:
I tried run the coding but unfortunately, it only works for a single file insertion,it just inserts the last file that is in the directory.
can help ?
0
 
Dave ITnewbie ITAuthor Commented:
Because i have file1.csv , file2.csv , file3.csv but the only file insert are file3.csv
0
 
Julian HansenCommented:
Because i have file1.csv , file2.csv , file3.csv but the only file insert are file3.csv

That is because the file control has the name file and attribute multiple. When it uploads it will upload all three files under the name file and PHP will only recognise the last one.

To fix this you need to declare your <input> with an array name
<input type="file" multiple name="file[]" />

Open in new window


Now when your form submits you will get something like this
Array
(
  [file] => Array
    (
      [name] => Array
        (
          [0] => 01.csv
          [1] => 02.csv
          [2] => 03.csv
          [3] => 04.csv
          [4] => 05.csv
        )
      [type] => Array
        (
          [0] => application/vnd.ms-excel
          [1] => application/vnd.ms-excel
          [2] => application/vnd.ms-excel
          [3] => application/vnd.ms-excel
          [4] => application/vnd.ms-excel
        )
      [tmp_name] => Array
        (
          [0] => C:\Windows\Temp\php5FEB.tmp
          [1] => C:\Windows\Temp\php5FEC.tmp
          [2] => C:\Windows\Temp\php5FED.tmp
          [3] => C:\Windows\Temp\php5FEE.tmp
          [4] => C:\Windows\Temp\php5FEF.tmp
        )
      [error] => Array
        (
          [0] => 0
          [1] => 0
          [2] => 0
          [3] => 0
          [4] => 0
        )
      [size] => Array
        (
          [0] => 170516
          [1] => 116832
          [2] => 2351
          [3] => 275819
          [4] => 1870
        )
    )
)

Open in new window


You will now need to iterate over one of the arrays in the $__FILES['file'] array - I would suggest the name array.
$files = isset($_FILES['file']) ? $_FILES['file'] : false;
if (!$files) {
   //error exit here
}
// Handle multiple upload
if (is_array($files['name'])) {
   foreach($files['name'] as $key => $f) {
      if (is_uploaded_file($files['tmp_name'][$key])) {
          move_uploaded_file($files['tmp_name'][$key], $f);
      }
   }
}
// Handle single upload
else {
      if (is_uploaded_file($files['tmp_name'])) {
          move_uploaded_file($files['tmp_name'], $files['name']);
      }
}

Open in new window

1
 
gr8gonzoConsultantCommented:
Recommend you accept Julian's answer instead, or at the very least, accept both. I can un-accepted your currently-accepted answer. Please let us know if you're okay with this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.