iojbr Posted April 14, 2015 Share Posted April 14, 2015 Hi: I am a newb to php. I am using the following code snippet (see below) to create an html form that will open a file browser, allow the user to select a file (.csv), and input that file path into a textbox on the form. How can I use php to extract this file path from the text box. and import the specified file into a MySQL table? I have been reading up on the fOpen function, but I am difficulty figuring out how to accomplish what I want with this function. Thank you for your help. <form action="php_script.php" method="post"> <input type="file" name="file_upload" /> <input type="submit" name="submit" value=" Submit" value=" Submit " /> < /form> Quote Link to comment https://forums.phpfreaks.com/topic/295552-import-csv-file-into-mysql-with-file-path-in-a-textbox/ Share on other sites More sharing options...
Zane Posted April 14, 2015 Share Posted April 14, 2015 Look at str_getcsv() error_reporting(E_ALL); ini_set('display_errors', true); $file="spreadsheet-file.csv"; $csv= file_get_contents($file); $array = array_map("str_getcsv", explode("\n", $csv)); echo "<pre>", print_r($array), "</pre>"; $tdata = null; foreach($array as $k=>$v) { $tdata[$k][] = $v; } echo "<pre>", print_r($tdata), "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/295552-import-csv-file-into-mysql-with-file-path-in-a-textbox/#findComment-1509046 Share on other sites More sharing options...
Barand Posted April 14, 2015 Share Posted April 14, 2015 You also need to look at how to upload a file. http://php.net/manual/en/features.file-upload.post-method.php Quote Link to comment https://forums.phpfreaks.com/topic/295552-import-csv-file-into-mysql-with-file-path-in-a-textbox/#findComment-1509051 Share on other sites More sharing options...
Psycho Posted April 14, 2015 Share Posted April 14, 2015 Here's a simple tutorial: http://www.tizag.com/phpT/fileupload.php Quote Link to comment https://forums.phpfreaks.com/topic/295552-import-csv-file-into-mysql-with-file-path-in-a-textbox/#findComment-1509053 Share on other sites More sharing options...
iojbr Posted April 20, 2015 Author Share Posted April 20, 2015 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: <!-- The data encoding type, enctype, MUST be specified as below --> <form enctype="multipart/form-data" action="csv_import_connect_db.php" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name="csv_file" type="file" /> <input type="submit" value="Send File" /> </form> 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']); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/295552-import-csv-file-into-mysql-with-file-path-in-a-textbox/#findComment-1509426 Share on other sites More sharing options...
Solution Muddy_Funster Posted April 20, 2015 Solution Share Posted April 20, 2015 that's not an error message (it even tells you error => 0) - what you are seeing is, I believe, the content of print_r($_FILES['csv_file']); Quote Link to comment https://forums.phpfreaks.com/topic/295552-import-csv-file-into-mysql-with-file-path-in-a-textbox/#findComment-1509429 Share on other sites More sharing options...
iojbr Posted April 21, 2015 Author Share Posted April 21, 2015 Got it! Thanks. God I feel stupid. Quote Link to comment https://forums.phpfreaks.com/topic/295552-import-csv-file-into-mysql-with-file-path-in-a-textbox/#findComment-1509503 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.