Avatar of Yong Scott
Yong Scott
Flag for Malaysia

asked on 

I upload file from directory to database but i realise that all file i upload is the same.

How to fix this .. can upload the directory but all the csv file content i upload was the same file  ..
Actually there are 30 files , but one file uploaded 30 times.
Here are my code for upload part .. upload.php
<?php
include 'config.php';

if(isset($_POST['btn-upload']))
{    
  $log_directory = '/xampp/htdocs/mydoc';
  $results_array = array();

        if ($handle = opendir($log_directory))
        {
                while(($file = readdir($handle)) !== FALSE)
                {
                    if ($file != "." && $file != "..") {
                        $results_array[] = $file;
                    }
                }
                closedir($handle);
        }

  //Output findings
  foreach($results_array as $value)
{
 $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
    if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
        if(is_uploaded_file($_FILES['file']['tmp_name'])){
            
            //open uploaded csv file with read only mode
            $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
            
            //skip first line
            fgetcsv($csvFile);
            
            //parse data from csv file line by line
            while(($line = fgetcsv($csvFile)) !== FALSE){
                 //insert member data into database
                    $db->query("INSERT INTO nov(f_name,name, cpu_count, memory_size, disk_space_size, nic_count, power_state) VALUES ('".$value."','".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."','".$line[5]."')");
                }
            }
            			
            //close opened csv file
            fclose($csvFile);


        


 if(move_uploaded_file)
 {
	 
 ?>
  <script>
  alert('successfully uploaded');
        window.location.href='index.php?success';
        </script>
  <?php
 }
 else
 {
  ?>
  <script>
  alert('error while uploading file');
        window.location.href='index.php?fail';
        </script>
		
  <?php
 }
}
}
}

?>

Open in new window

DatabasesPHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon