NYCPHP Meetup

NYPHP.org

[nycphp-talk] Exporting csv

Phillip Powell phillip.powell at adnet-sys.com
Tue Sep 21 17:36:56 EDT 2004


Darian V. Schramm wrote:

>Hello List, 
>
>I'm trying to use the header() php function to output a csv from the results
>of a mysql query. There was a post on here not long ago about forcing downloads
>of a file on the filesystem. Is it possible to force a download of a string? 
>  
>
Yes.  I have it within my application to do just that by manipulating 
the headers as so:

    /**
     * Generate the HTTP headers necessary for this file type
     *
     * @access public
     * @param mixed $filename
     */
    function &generateHTTPHeaders($filename) {                    // 
STATIC VOID METHOD

        if (!preg_match('/.+\.[a-zA-Z0-9\-_]+$/i', $filename))
         die("Filename: \"$filename\" must have an extension");
        $ext = substr($filename, strrpos($filename, '.') + 1, 
strlen($filename));

        switch (strtolower(trim($ext))) {
             case 'pdf':
          $ctype = 'application/pdf';
              break;
         case 'exe':
          $ctype = 'application/octet-stream';
          break;
         case 'zip':
          $ctype = 'appliation/zip';
          break;
         case 'doc':
          $ctype = 'application/msword';
          break;
         case 'xls':
          $ctype = 'application/vnd.ms-excel';
          break;
         case 'csv':
          $ctype = 'application/vnd.ms-excel';
          break;
          case 'ppt':
          $ctype = 'application/vnd.ms-powerpoint';
          break;
         case 'gif':
          $ctype = 'image/gif';
          break;
         case 'png':
          $ctype = 'image/png';
          break;
         case 'jpg':
          $ctype = 'image/jpg';
          break;
           case 'jpeg':
          $ctype = 'image/jpg';
          break;
         default:
          $ctype = 'application/force-download';
          break;
        }

        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Type: $ctype");
        $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if ((is_integer(strpos($user_agent, 'msie'))) && 
(is_integer(strpos($user_agent, 'win')))) {
            header('Content-Disposition: filename=' . 
basename($filename) . ';');
        } else {
            header('Content-Disposition: inline; filename=' . 
basename($filename) . ';');
        }
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($filename));
        if (function_exists('file_get_contents')) {
         echo file_get_contents($filename);
        } else {
         readfile($filename);
        }
    }

This is actually a class method within a class I wrote that generates 
reports.

Phil

>Here's my code:
>
><?
>include ('includes/configure.php');
>include ('includes/classes/db_utils.php');
>
>function outOfStock(){
>
>$query = "select sku_code, products_quantity, quantity_threshold, products_name, attribute_name from products p join products_description pd on p.products_i
>d=pd.products_id where products_status='1' and products_quantity <= quantity_threshold order by products_name";
>
>$rows = DB_UTILS::queryMultiRow($query);
>foreach($rows as $row){
>    $output .= implode(",",$row);
>    $output .= "\n";
>}
> return $output;
>}
>
>
>$date = gmdate("m-d-Y",time());
>$file = outOfStock();
>$filesize = strlen($file);
>$filename = "out-of-stock-" . $date;
>
>header("Pragma: public");
>header("Expires: 0");
>header("Cache-Control: private");
>header("Content-Type: text/csv");
>header("Content-Disposition: attachment; filename=$filename");
>header("Accept-Ranges: bytes");
>header("Content-Length: $download_size"); 
>
>echo $file;
>exit();
>
>?>
>
>It would be possible to write a temp file with the data, but it seems like there 
>has to be away to do this.
>
>Thanks in advance,
>
>-
>darian <darian at slackworks.com>
>
>_______________________________________________
>New York PHP Talk
>Supporting AMP Technology (Apache/MySQL/PHP)
>http://lists.nyphp.org/mailman/listinfo/talk
>http://www.newyorkphp.org
>
>  
>


-- 
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
BPX Technologies, Inc.
#: (703) 709-7218 x107 
Fax: (703) 709-7219

	




More information about the talk mailing list