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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.