Answers for "php download to csv"

PHP
1

php save csv to zip

// Create ZIP file
$zipname = storage_path() . '/' . 'zipName.zip';
$zip = new \ZipArchive;

if ($zip->open($zipname, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) != true) {
  throw new \Exception('Can\'t save zip file');
}

// Create CSV file
$columns = ['id', 'uid', 'name', 'dob'];
$clientCvs = fopen('php://temp/maxmemory:1048576', 'w');
if (false === $clientCvs) {
  throw new \Exception('Failed to create temporary file');
}

fputcsv($clientCvs, $columns);
fputcsv($clientCvs, [
  $this->id,
  $this->uid,
  $this->name,
  $this->dob ?? '-',
]);

// Rewind stream source
rewind($clientCvs);

// Add to the zip file
$zip->addFromString('member.csv', stream_get_contents($clientCvs));

// Close temp file
fclose($clientCvs);
Posted by: Guest on September-02-2021
0

download csv php mysql

CREATE TABLE `users` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `username` varchar(80) NOT NULL,
  `name` varchar(50) NOT NULL,
  `gender` varchar(10) NOT NULL,
  `email` varchar(70) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

=========================================================
// this config.php
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}


=================================================================
// prints contents of db to a table with an export button
<?php 
include "config.php"; // Database connection file
?>

<div class="container">
 
 <form method='post' action='download.php'>
  <input type='submit' value='Export' name='Export'>
 
  <table border='1' style='border-collapse:collapse;'>
    <tr>
     <th>ID</th>
     <th>Username</th>
     <th>Name</th>
     <th>Gender</th>
     <th>Email</th>
    </tr>
    <?php 
     $query = "SELECT * FROM users ORDER BY id asc";
     $result = mysqli_query($con,$query);
     $user_arr = array();
     while($row = mysqli_fetch_array($result)){
      $id = $row['id'];
      $uname = $row['username'];
      $name = $row['name'];
      $gender = $row['gender'];
      $email = $row['email'];
      $user_arr[] = array($id,$uname,$name,$gender,$email);
   ?>
      <tr>
       <td><?php echo $id; ?></td>
       <td><?php echo $uname; ?></td>
       <td><?php echo $name; ?></td>
       <td><?php echo $gender; ?></td>
       <td><?php echo $email; ?></td>
      </tr>
   <?php
    }
   ?>
   </table>
   <?php 
    $serialize_user_arr = serialize($user_arr);
   ?>
  <textarea name='export_data' style='display: none;'><?php echo $serialize_user_arr; ?></textarea>
 </form>
</div>
    
    
    
    =============================================================
  //Create a new download.php file -- code below//  
    
    <?php
$filename = 'users.csv';
$export_data = unserialize($_POST['export_data']);

// file creation
$file = fopen($filename,"w");

foreach ($export_data as $line){
 fputcsv($file,$line);
}

fclose($file);

// download
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; "); 

readfile($filename);

// deleting file
unlink($filename);
exit();
Posted by: Guest on March-27-2020

Browse Popular Code Answers by Language