get file path from text box to import csv file

The code snippet below that I got online will open a file browser window, allow the user to select a file, and then input the file path into a text box in a html form. How do I define this file path as a variable using php and import this file into a MySQL table. Thanks in advance.

There are lots of tutorials available - what have you tried, and what are you having trouble with?

Here’s a sample : http://www.infotuts.com/import-csv-file-data-in-mysql-php/

Thanks for the reply. This is my code so far. I got as far as uploading a test csv file into the default htdocs directory in Apache, but it gave me a error message:

/Array ( [name] => Test File.csv [type] => application/vnd.ms-excel [tmp_name] => C:\Windows\Temp\phpF029.tmp [error] => 0 [size] => 54 )

I have included the code for the two .php files below. Thanks for replying.

1) first php file for opening a file browser window to select the .csv file I want to import:

Send this file:

2) The second php file for uploading that file into the htdocs/csv_dir folder:

/<?php
//conection:
$link = mysqli_connect(“localhost”,“root”,“pancor01”,“Test”) or die("Error " . mysqli_error($link));

//check for file upload

if(isset($_FILES[‘csv_file’]) && is_uploaded_file($_FILES[‘csv_file’][‘tmp_name’])){

//upload directory

$upload_dir = “csv_dir/”;

//create file name

$file_path = $upload_dir . $_FILES[‘csv_file’][‘name’];

//move uploaded file to upload dir

if (!move_uploaded_file($_FILES[‘csv_file’][‘tmp_name’], $file_path)) {

//error moving upload file

echo “Error moving file upload”;

} else {

print_r($_FILES[‘csv_file’]);

}

}

?>

The size is very small, so it did not upload properly. Check to see if the tmp file contains any messages.
Make sure you test it with small files to make sure that it works.

Wrap the code in

[code]
try {

} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), “\n”;
}
[/code] to see if you get a better message, and report where the error appears.

Sponsor our Newsletter | Privacy Policy | Terms of Service