candice Posted September 11, 2008 Share Posted September 11, 2008 Hi all, I am importing a csv file into my sql database and I am currently using the LOAD data infile function. However, it doesnt seem to detect the location of the file even though i have specified it in the query. Can anybody help? Attached below are my codes: <?php session_start(); echo $name; ob_start(); ?> <?php require_once('connection.php'); ?> <?php //UPLOADING FILE if(isset($_POST['upload'])) { $uploaddir = 'C:\wamp\www\New admin Site/uploads/'; $uploadfile = $uploaddir . basename($_FILES['fileupload']['name']); $file = array($uploadfile);//store the file into an array echo '<pre>'; if (move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); print "</pre>"; } $fcontents = $_SESSION[csvfile]; for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode("\t", $line); echo $arr."<br>\n"; } //ATTRIBUTES $fieldseparator = ','; $lineseparator = '\n'; //READING OF FILE $csvfile = $uploaddir . basename($_FILES['fileupload']['name']); //TRUNCATE THE FILE BEFORE INSERTING IT INTO THE DATABASE mysql_select_db($database, $test); mysql_query("TRUNCATE TABLE timetable") or die("MySQL Error: " . mysql_error()); //Delete the existing rows //IMPORT CSV INTO DATABASE $lines = 0; $queries = ""; $linearray = array(); $fcontents = file($csvfile); for($i=0; $i<sizeof($fcontents); $i++) { $lines++; $line = trim($fcontents[$i]); $linearray = explode(',',$line); $linemysql = implode("','",$linearray); // convert the array to a string echo "$linemysql". "\n"; //$query = "INSERT INTO timetable values('$linemysql')";//Insert query to insert values into the database $query = 'LOAD DATA INFILE "C:\wamp\www\New admin Site\uploads\testing.csv" INTO TABLE timetable FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r\n"'; mysql_query($query, $test) or die('SQL ERROR:'.mysql_error()); //Insert in the new values into the database } ?> </head> <body> </body> </html> <?php ob_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/123801-load-data-infile-error/ Share on other sites More sharing options...
Mchl Posted September 11, 2008 Share Posted September 11, 2008 I'm not sure you can have spaces in the path to the file. Do you get a MySQL error? Link to comment https://forums.phpfreaks.com/topic/123801-load-data-infile-error/#findComment-639206 Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2008 Share Posted September 11, 2008 \ is an escape character. You would either need to double them \\ or use / Link to comment https://forums.phpfreaks.com/topic/123801-load-data-infile-error/#findComment-639292 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.