Jump to content

How do I pass a file path from an file field to a LOAD DATA INFILE query in php?


candice

Recommended Posts

Hi all, I'm currently developing a php application where users can upload the csv file into the databse. However, i am facing this problem where i cant pass the file path from the file field to the query that loads the csv data into the database. I'm using a LOAD DATA INFILE query to load the csv file data into the database. Can anyone help??

 

Thanks in advance!

 

Candice

What have you done to troubleshoot this? Do you know that the upload was successful? Have you echoed out the query so you know it contains what you expect? Is the query failing with an error?

 

You would need to post your code for anyone to be able help with what it is doing.

When the file is successfully uploaded, a message would appear that shows the debugging info of the file. Currently the variable that i have assigned the file path to, is unable to track and store the file path of the file. Below is the code that i used

 

<?php 
session_start();
echo $name;
ob_start();
?>

<?php require_once('connection.php'); ?>

<?php 

//UPLOADING FILE
if(isset($_POST['upload']))
{

$uploaddir = 'C:/wamp/www/Test upload/uploads';

$uploadfile = $uploaddir . basename($_FILES['fileupload']['name']);

$target_path = ini_get('$uploadfile');

//echo $target_path;

//$file = array($uploadfile);//store the file into an array

echo $file;

echo '<pre>';
if (move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile)) {

	//$target_path = 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 "$target_path" REPLACE
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(); ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.