Created
July 15, 2014 09:59
-
-
Save IsmiKin/474eb5d0fe0cb4d8fb17 to your computer and use it in GitHub Desktop.
Import a CSV file in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $success ; | |
$success = 0; | |
if(isset($_FILES['uploadedfile'])){ | |
$target_path = "query/import/"; | |
/* Add the original filename to our target path. | |
Result is "uploads/filename.extension" */ | |
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); | |
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { | |
/* echo "The file ". basename( $_FILES['uploadedfile']['name']). | |
" has been uploaded";*/ | |
include("query/connection.php"); | |
$nombrecito = $_FILES['uploadedfile']['name']; | |
//echo "<br/><br/><br/>".$nombrecito ; | |
$option = $_POST["tableforcsv"]; | |
if($option == "Rooms"){ | |
$consulta = "load data local infile '$nombrecito' into table Room fields terminated by ',' enclosed by '\"' lines terminated by '\n' IGNORE 1 LINES (name, capacity) "; | |
}else if($option=="Courses"){ | |
$consulta = "load data local infile '$nombrecito' into table Room fields terminated by ',' enclosed by '\"' lines terminated by '\n' IGNORE 1 LINES (name, capacity) "; | |
} | |
if($db->query($consulta)){ | |
$success =1; | |
}else{ | |
$success = 2; | |
} | |
// echo $consulta; | |
} else{ | |
/*echo "There was an error uploading the file, please try again!";*/ | |
} | |
} | |
?> | |
<!-- Insertamos el codigo JS exclusivo para ESTA pagina --> | |
<div class="container-mainpage"> | |
<div class="form-new-course"> | |
<h4><span class="glyphicon glyphicon-cloud-upload"></span> Import File</h4> | |
<hr> | |
<!-- <form class="form-inline" role="form" method="post" action="query/insertCourse.php" > --> | |
<form enctype="multipart/form-data" action="importcsv.php" method="POST" role="form" > | |
<div class="form-group"> | |
<input type="hidden" name="MAX_FILE_SIZE" value="100000" /> | |
<label class="sr-only" for="nameInput">Name Course</label> | |
<div class="form-group"> | |
<select class="form-control" id="options" name="tableforcsv"> | |
<option value="Rooms">Rooms</option> | |
<option value="Courses">Courses</option> | |
</select> | |
</div> | |
<input type="file" name="uploadedfile" id="importBtn" ></input> | |
<input type="submit" value="Upload File" /> | |
</div> | |
<div class="alert alert-success" <?php if(!$success==1){ ?> hidden="hidden" <?php } ?>>The import was succesful</div> | |
<div class="alert alert-danger" <?php if(!$success==2){ ?> hidden="hidden" <?php } ?>>It was ocurred an error </div> | |
<div class="alert alert-warning" <?php if(!$success==0){ ?> hidden="hidden" <?php } ?>>Any file inserted. </div> | |
</form> | |
<strong>IMPORTANT NOTE:</strong> <br/> | |
The ID of Courses is an Integer, then you have to change the CSV id to match with the Database<br/> | |
<font color="red">(Not implemented)</font>The ID of Teachers is an Integer, then the FK will not match neither, then you have to change for | |
integers which exists in the Database | |
</div> | |
</div> | |
<?php | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment