-4
<?php
require_once("database.php");

if(@$_POST['upload']) {

    $file_name      = $_FILES['cvs']['name'];
    $file_type      = $_FILES['cvs']['type'];
    $file_temp_loc  = $_FILES['cvs']['tmp_name'];
    $file_error_msg = $_FILES['cvs']['error'];
    $file_size      = $_FILES['cvs']['size'];

    /* 1. file upload handling */
    if(!$file_temp_loc) { // if not file selected
        echo "Error: please browse for a file before clicking the upload button.";
        exit();
    }
    if(!preg_match("/\.(csv)$/i", $file_name)) { // check file extension
        echo 'Error: your file is not CSV.';
        @unlink($file_temp_loc); // remove to the temp folder
        exit();
    }
    if($file_size > 5242880) { // file check size
        echo "Error: you file was larger than 5 Megabytes in size.";
        exit();
    }
    if($file_error_msg == 1) { //
        echo "Error: an error occured while processing the file, try agian.";
        exit();
    }

    $move_file = move_uploaded_file($file_temp_loc, "\\Reformist003-pc/c/xampp/htdocs/FARMAPP/Farmer/{$file_name}"); // temp loc, file name
    if($move_file != true) { // if not move to the temp location
        echo 'Error: File not uploaded, try again.';
        @unlink($file_temp_loc); // remove to the temp folder
        exit();
    }

    $csvFile  = '\\Reformist003-pc/c/xampp/htdocs/FARMAPP/Farmer/'.$file_name;
    $csvFileLength = filesize($csvFile);
    $csvSeparator = ",";
    $handle = fopen($csvFile, 'r');

    $count = '';
    while($data = fgetcsv($handle, $csvFileLength, $csvSeparator)) { // while for each row
        $count += count($data[0]); // count imported
        mysql_query("INSERT INTO `csv_data` (`product_title`, `product_price`) VALUES ( '$data[0]', '$data[1]' )");
    }

    fclose($handle);
    unlink($csvFile); // delete cvs after imported
    header('Location: index.php?success=1&count='.$count);
    exit();
}
?>
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91

1 Answers1

0

please check the spelling of file field's name .. it must be csv in html and you called it cvs

$file_name      = $_FILES['csv']['name'];
$file_type      = $_FILES['csv']['type'];
$file_temp_loc  = $_FILES['csv']['tmp_name'];
$file_error_msg = $_FILES['csv']['error'];
$file_size      = $_FILES['csv']['size'];
Muhammad Ali
  • 1,992
  • 1
  • 13
  • 20