I have a table name tblstuden. It contains ids studentname, rollnumberm and classname. I want to download this data in CSV format. How is it possible?
I can upload the data into the table by CSV file. To upload the code is below. It's working for me. But now I want to download the data in CSV format.
<?php
if(isset($_POST['submit']))
{
$row = 1;
$file1 = $_FILES['file']['tmp_name'];
$file = fopen($file1, "r");
while (($data = fgetcsv($file, 8000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
$data[$c];}
echo $data[0];
$name = $data[0];
}
fclose($file);
?>
You can download a csv file using this code.
$getRecord = mysql_query("SELECT id, studentname, rollnumber, classname FROM tblstuden");
while ($row = mysql_fetch_array($getRecord )) {
$csvFile .= $row['id'].",". $row['studentname'].",". $row['rollnumber'].",".$row['classname']."\r\n";
}
header("Content-type: application/octet-stream");
header("Content-disposition:attachment; filename="."testCSV.csv");
echo $csvFile;
Related
I want to import CSV file data to MySQL.
This is the code:
if (is_uploaded_file($_FILES['excelfile']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['excelfile']['name'] ." Upload Done!" . "</h1>";
echo "<h2>File Upload:</h2>";
readfile($_FILES['excelfile']['tmp_name']);
}
$handle = fopen($_FILES['excelfile']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$import="INSERT into city(city_name,days) values('$data[0]','$data[1]')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
echo "<br><strong>Import Done.</strong><br>
The result just inserts into one column:
The fields in your CSV file are separated by semicolon, but you're using comma in your call to fgetcsv(). Try
while ($data = fgetcsv($handle, 1000, ";")) {
You can get the data on CSV file following code.
if ($_FILES['excelfile']['size'] > 0) {
$mimes = array('application/vnd.ms-excel', 'text/plain', 'text/csv', 'text/tsv');
//get the csv file
$file = $_FILES['excelfile']['tmp_name'];
$handle = fopen($file, "r");
$data = array();
while (!feof($handle)) {
$data[] = fgetcsv($handle);
}
}
Now you have data of CSV on $data array.So using foreach and then you can insert data into database.
I have a CSV list which contains URL/Link of some image.
So I had tried this below code;
if (($handle = fopen($csv_file, "r")) !== FALSE) {
fgetcsv($handle);
$num = count($data);
for ($c=0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
col1 = $col[0];
for ($i = 0; $i < count($col1); $i++){
if ( !$col1[$i] == null){
echo $col1[$i]. ",<br>";
file_put_contents("images/img.jpg", $elements[$i] . "\n", FILE_APPEND);
}
}
}
fclose($handle);
}
the code is working, but it's replacing the image at the end of execution I'm getting only one pic which's Last row of csv & Getting error Max_execution time.
Try this out.
$handle = fopen($csv_file, "r");
$destination = 'images/';
if ($handle) {
while ($columns = fgetcsv($handle)) {
foreach ($columns as $imageUrl) {
if (!empty($imageUrl)) {
file_put_contents(
$destination . basename($imageUrl),
file_get_contents($imageUrl)
);
}
}
}
}
What it does is open the CVS, loops through it, row at a time, checks that the image URL isn't empty, downloads it and puts it into your directory. You of course need to make sure that PHP has the rights to write to that directory. The code also doesn't handle conflicts in the naming of images, so a further improvement would be to add some clash detection and append a counter before the suffix.
I'm trying to recieve data from a .csv. Here the code I have so far:
if(($handle = fopen("test.csv", "r")) !== FALSE){
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){
}
}
But this puts all the data I need in the first entry in the $data array. What I need to do is $data[1] and then get all the values from the second column. I've done it before but I can't it to work this time. How do I do it?
EDIT
Here is my .csv file structure
| ID | Name |
|------|----------|etc
| 1 |Product 1 |
Here's a screenshot of the file http://imgur.com/BlaCvjo
And with echo $data[1] it should return 'Product 1'
Please be specific to your question. Anyway, these codes will help you to your question.
<?php
$csv = array();
// check there are no errors
if($_FILES['csv']['error'] == 0){
$name = $_FILES['csv']['name'];
$ext = strtolower(end(explode('.', $_FILES['csv']['name'])));
$type = $_FILES['csv']['type'];
$tmpName = $_FILES['csv']['tmp_name'];
// check the file is a csv
if($ext === 'csv'){
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
set_time_limit(0);
$row = 0;
while(($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// number of fields in the csv
$col_count = count($data);
// get the values from the csv
$col1 = $data[0];
$col2 = $data[1];
$col3 = $data[2];
$col4 = $data[2];
// inc the row
$row++;
}
fclose($handle);
}
}
}
?>
Variables:
$csvFile = $_FILES['uploadfile']['tmp_name'];
$csvFile = $con->real_escape_string($csvFile);
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv','csv');
if statement:
if(in_array($csvFile, $mimes))
{
$file = fopen($csvFile, 'r');
$i = 0;
$row = 1;
while (($line = fgetcsv($file)) !== FALSE)
{
if($row == 1){ $row++; continue; }
$data_array[$i] = $line[0];
$data_array[$i] = mysqli_real_escape_string($con, trim($data_array[$i]));
$encrypted_numbers[$i] = encryption::encrypt($data_array[$i]);
$encrypted_numbers[$i] .= mysqli_real_escape_string($con, trim($line[1]));
$i++;
}
fclose($file);
}
else
{
die("Sorry, file type not allowed");
};
I've been trying stuff for 30 min, anyone know why I always get false even if the file is named csv.csv?
$_FILES['uploadfile']['tmp_name'];
you should be looking in 'type' not 'tmp_name'
$_FILES['uploadfile']['type'];
However, there is an SO discussion here about why a csv file can have the application/octet-stream mime-type. It suggests that you need to verify the file-type yourself, and not rely on ['type']. The best way would be to use fgetcsv() to try to parse it as csv. If this fails then assume it is not a csv.
I am reading content from a csv file with
$row = 1;
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { //Reading the file
$num = count($data);
for ($c = 0; $c < $num; $c++) {
$csvData[$row][$c] = $data[$c];
}
$row++;
}
return $csvData;
fclose($handle);
}
but if I create csv on MAC at the end of each row there is CRLF but on windows there is only CR and my script read fine only for windows. How could I fix my script for MAC as well.