I built this script to upload .csv files and directly send them to my database. But it doesn't write anything into my table. If i uploade the file to my server and write "file.csv" instead of "$file" it works perfectly.. But I want to upload them via form. Can anyone help me?
Form:
<html>
<head>
</head>
<body>
<h2> CSV Import </h2>
<form>
<form method="post" action="imp.php" enctype="multipart/form-data">
<input type="file" name="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
imp.php:
<?php
$delimiter = ',';
$db = new mysqli('host', 'user', 'pw', 'db');
$file= $_FILES[file][temp_name];
if (($handle = fopen($file, 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
foreach($data as $i => $content) {
$data[$i] = $db->real_escape_string($content);
}
$db->query("INSERT INTO table VALUES('" . implode("','", $data) . "');");
}
fclose($handle);
}
?>