lovephp Posted April 3, 2014 Share Posted April 3, 2014 (edited) friend i got this importing csv to database working fine but how could i make it that i choose the csv file from a html form and then upload please help me out <?php //database connection details require_once('config.php'); // path where your CSV file is located define('CSV_PATH','C:\Users\Downloads/'); // Name of your CSV file $csv_file = CSV_PATH . "student.csv"; if (($getfile = fopen($csv_file, "r")) !== FALSE) { $data = fgetcsv($getfile, 100000000, ","); while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { $result = $data; $str = implode(",", $result); $slice = explode(",", $str); $col0 = $slice[0]; $col1 = $slice[1]; $col2 = $slice[2]; $col3 = $slice[3]; $col4 = $slice[4]; $col5 = $slice[5]; $col6 = $slice[6]; $col7 = $slice[7]; // SQL Query to insert data into DataBase $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip) VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')"; $s=mysql_query($query); } } } echo "File data successfully imported to database!!"; ?> regards and thanks Edited April 3, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/ Share on other sites More sharing options...
ginerjm Posted April 3, 2014 Share Posted April 3, 2014 Hard to help you with your coding when you don't show any. Read up on how it's done, write something and see what happens. Then if it doesn't work, we can help you. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474847 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 (edited) tried but not working so asked for help <?php //database connection details require_once('config.php'); $uploaddir = 'uploads'; $csv_file = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { unlink($uploadfile); } if (($getfile = fopen($csv_file, "r")) !== FALSE) { $data = fgetcsv($getfile, 100000000, ","); while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { $result = $data; $str = implode(",", $result); $slice = explode(",", $str); $col0 = $slice[0]; $col1 = $slice[1]; $col2 = $slice[2]; $col3 = $slice[3]; $col4 = $slice[4]; $col5 = $slice[5]; $col6 = $slice[6]; $col7 = $slice[7]; // SQL Query to insert data into DataBase $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip) VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')"; $s=mysql_query($query); } } } //echo "File data successfully imported to database!!"; ?> <form enctype="multipart/form-data" action="mysqldump.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000000" /> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form> Edited April 3, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474848 Share on other sites More sharing options...
iarp Posted April 3, 2014 Share Posted April 3, 2014 Where does $uploadfile come from? I don't see if being initialized anywhere. Unlink also deletes the file from the server, and with your current logic it basically says "if move this file was successful, delete it" Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474852 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 ok i tried this <?php require_once('config.php'); if(isset($_FILES['csv'])){ $errors= array(); $file_name = $_FILES['csv']['name']; $file_size =$_FILES['csv']['size']; $file_tmp =$_FILES['csv']['tmp_name']; $file_type=$_FILES['csv']['type']; $file_ext=strtolower(end(explode('.',$_FILES['csv']['name']))); $extensions = array("csv"); if(in_array($file_ext,$extensions )=== false){ $errors ="Unknown file extensions, Upload only .csv file"; } if(empty($errors)==true){ $csv_file = move_uploaded_file($file_tmp,"uploads/".$file_name); if (($getfile = fopen($csv_file, "r")) !== FALSE) { $data = fgetcsv($getfile, 100000000, ","); while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { $result = $data; $str = implode(",", $result); $slice = explode(",", $str); $col0 = $slice[0]; $col1 = $slice[1]; $col2 = $slice[2]; $col3 = $slice[3]; $col4 = $slice[4]; $col5 = $slice[5]; $col6 = $slice[6]; $col7 = $slice[7]; // SQL Query to insert data into DataBase $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip) VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')"; $s=mysql_query($query); } } } }else{ echo $errors; } } ?> <form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="csv" /> <input type="submit"/> </form> file gets uploaded but nothing into database gets inserted Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474859 Share on other sites More sharing options...
DavidAM Posted April 3, 2014 Share Posted April 3, 2014 $csv_file = move_uploaded_file($file_tmp,"uploads/".$file_name); if (($getfile = fopen($csv_file, "r")) !== FALSE) { move_uploaded_file returns TRUE or FALSE, not the name of the file. You should test if move_uploaded_file() succeeds, then open "uploads/".$file_name. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474862 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 (edited) done did it atlast but the file is not getting deleted after inserting into db <?php require_once('config.php'); if(isset($_FILES['csv'])){ $errors= array(); $file_name = $_FILES['csv']['name']; $file_size =$_FILES['csv']['size']; $file_tmp =$_FILES['csv']['tmp_name']; $file_type=$_FILES['csv']['type']; $file_ext=strtolower(end(explode('.',$_FILES['csv']['name']))); $extensions = array("csv"); if(in_array($file_ext,$extensions )=== false){ $errors ="Unknown file extensions, Upload only .csv file"; } if(empty($errors)==true){ move_uploaded_file($file_tmp,"uploads/".$file_name); if (($getfile = fopen("uploads/".$file_name, "r")) !== FALSE) { $data = fgetcsv($getfile, 100000000, ","); while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) { $num = count($data); for ($c=0; $c < $num; $c++) { $result = $data; $str = implode(",", $result); $slice = explode(",", $str); $col0 = $slice[0]; $col1 = $slice[1]; $col2 = $slice[2]; $col3 = $slice[3]; $col4 = $slice[4]; $col5 = $slice[5]; $col6 = $slice[6]; $col7 = $slice[7]; // SQL Query to insert data into DataBase $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip) VALUES('".$col0."','".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')"; $s=mysql_query($query); } } } unlink("uploads/$file_name"); }else{ echo $errors; } } ?> <form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="csv" /> <input type="submit"/> </form> Edited April 3, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474864 Share on other sites More sharing options...
ginerjm Posted April 3, 2014 Share Posted April 3, 2014 You didnt' give us an error so we don't know what's happening, but let me provide a little improvement on your code that may help you to get a better understanding of what you are doing. To process the contents of the csv file do this: while ($slice = fgetcsv($getfile, 100000000, ",")) { $col0 = $slice[0]; $col1 = $slice[1]; $col2 = $slice[2]; $col3 = $slice[3]; $col4 = $slice[4]; $col5 = $slice[5]; $col6 = $slice[6]; $col7 = $slice[7]; ... ... } A much clearer and sensible of retrieving the data read in. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474866 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 You didnt' give us an error so we don't know what's happening, but let me provide a little improvement on your code that may help you to get a better understanding of what you are doing. To process the contents of the csv file do this: while ($slice = fgetcsv($getfile, 100000000, ",")) { $col0 = $slice[0]; $col1 = $slice[1]; $col2 = $slice[2]; $col3 = $slice[3]; $col4 = $slice[4]; $col5 = $slice[5]; $col6 = $slice[6]; $col7 = $slice[7]; ... ... } A much clearer and sensible of retrieving the data read in. thanks bro ill do that and could you tell me why the uploaded file not getting deleted? Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474869 Share on other sites More sharing options...
ginerjm Posted April 3, 2014 Share Posted April 3, 2014 I don't see why - ttry adding an echo after it to see if it gets there. Also - just noticed that you are losing the first row of you csv file due to an extra read before your loop Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474876 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 yes i am losing the first row to remove the heading of file and i tried still the file remains in the folder dunno what is wrong im confused Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474877 Share on other sites More sharing options...
ginerjm Posted April 3, 2014 Share Posted April 3, 2014 did you add an echo to debug the code? Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474880 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 echo to which line? Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474883 Share on other sites More sharing options...
ginerjm Posted April 3, 2014 Share Posted April 3, 2014 Put an echo in front of the unlink and one after it to prove that it ran. Turn on php error checking too. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474885 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 error_reporting(1); and yes i did echo unlink("uploads/$file_name"); but no error show and it dumps data to mysql but file stays still in uploads folder Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474886 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 ok i did error_reporting(E_ALL); and i get permission denied. im using wamp how do i fix it? Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474887 Share on other sites More sharing options...
ginerjm Posted April 3, 2014 Share Posted April 3, 2014 error_reporting(E_ALL | E_STRICT | E_NOTICE); ini_set('display_errors', '1'); To repeat my previous put an echo IN FRONT and one AFTER the unlink. Showing the result of the unlink is not very helpful since it is Boolean most likely. echo "About to do unlink<br>"; unlink(....); echo "Did the unlink"; Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474888 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 (edited) error_reporting(E_ALL | E_STRICT | E_NOTICE); ini_set('display_errors', '1'); To repeat my previous put an echo IN FRONT and one AFTER the unlink. Showing the result of the unlink is not very helpful since it is Boolean most likely. echo "About to do unlink<br>"; unlink(....); echo "Did the unlink"; yes i did and i get Warning: unlink(uploads/student.csv) [function.unlink]: Permission denied in C:\wamp\www\student\sqldumup.php on line 35 this is line 35 echo unlink("uploads/$file_name"); Edited April 3, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474891 Share on other sites More sharing options...
DavidAM Posted April 3, 2014 Share Posted April 3, 2014 This is no real reason to move_uploaded_file() if you aren't going to keep it. Just open the temporary file. PHP will remove that one at the end of the script (supposedly). But maybe you need to close it before you unlink it. If this is Windows, I'm pretty sure that is the problem. As for processing ... while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) { if (count($data) > $data = array_slice($data, 0, ; $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip) VALUES('" . implode("','", $data) . "')"; $s=mysql_query($query); } } Actually, you would be better off using a prepared statement. And even better off by doing a multi-row insert. But since you are still using the mysql library, I'll leave it at that. Note: the mysql library has been deprecated. You need to switch to mysqli. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474893 Share on other sites More sharing options...
lovephp Posted April 3, 2014 Author Share Posted April 3, 2014 This is no real reason to move_uploaded_file() if you aren't going to keep it. Just open the temporary file. PHP will remove that one at the end of the script (supposedly). But maybe you need to close it before you unlink it. If this is Windows, I'm pretty sure that is the problem. As for processing ... while (($data = fgetcsv($getfile, 100000000, ",")) !== FALSE) { if (count($data) > $data = array_slice($data, 0, ; $query = "INSERT INTO details(id, student_phone, student_fname, student_lname, street_address, city, state, zip) VALUES('" . implode("','", $data) . "')"; $s=mysql_query($query); } } Actually, you would be better off using a prepared statement. And even better off by doing a multi-row insert. But since you are still using the mysql library, I'll leave it at that. Note: the mysql library has been deprecated. You need to switch to mysqli. thanks that was helpful. now how on earth i sort the unlink file. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474895 Share on other sites More sharing options...
DavidAM Posted April 4, 2014 Share Posted April 4, 2014 But maybe you need to close it before you unlink it. If this is Windows, I'm pretty sure that is the problem. thanks that was helpful. now how on earth i sort the unlink file. Did you try closing the file? On a *nix system if would not matter, but Windows is particular about that. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474907 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 (edited) To sort a csv file is a bit of a bind but do-able. Read the file in using fgetcsv which will give you an array. Then grab the element of the array that you want to sort by and then assign that value as the key of a new array and the array you read the data into as the value of that key. while ($row = fgetcsv($handle)) { // use element #3 of the input as the sort key (starting with 0) $sortarray[{$row[3]}] = $row; Now just sort the var $sortarray and then process the sorted array using a foreach. Of course I haven't done this and I'm having a bad day on the forums, so there could be flaws in this. Cheers! foreach ($sortarray as $k=>$v) { (process $v as an array the same as you would have processed $row in the first block of code) } Edited April 4, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474909 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 any help friends? Realy getting frustrated for not being able to unlink the file after processing Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474921 Share on other sites More sharing options...
lovephp Posted April 4, 2014 Author Share Posted April 4, 2014 thanks bro i knw im being a pain bt stil am jus no being able to get it dne. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474922 Share on other sites More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 I thought it was a permissions problem. Change the permissions and it will go away. OR use the advice given to you and skip the whole move and creating a permanent file and just let the system delete the temporary file. OH - and work on your English. Or stop typing on your cell phone. A little respect for yourself and those you wish to read and understand your writings can come in handy. Quote Link to comment https://forums.phpfreaks.com/topic/287498-little-help-please-need-to-make-file-get-uploaded-by-form/#findComment-1474938 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.