Solved

PHP/MySQL to create CSV in remote folder

Posted on 2012-04-10
5
289 Views
Last Modified: 2012-04-11
So i am trying to create a page that will query my database and export the data to a csv file in a remote folder... I can make it work all good, except it wont save it to the folder, it will just bring up the SAVE/OPEN box...  Script is below...

$query = sprintf( "SELECT * FROM table" );
$result = mysql_query( $query, $conn ) or die( mysql_error( $conn ) );

header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename=CUST.CSV' );

  $row = mysql_fetch_assoc( $result );
  if ( $row )
  {
    echocsv( array_keys( $row ) );
  }

  while ( $row )
  {
    echocsv( $row );
    $row = mysql_fetch_assoc( $result );
  }

  function echocsv( $fields )
  {
    $separator = '';
    foreach ( $fields as $field )
    {
      if ( preg_match( '/\\r|\\n|,|"/', $field ) )
      {
        $field = '"' . str_replace( '"', '""', $field ) . '"';
      }
      echo $separator . $field;
      $separator = ',';
    }
    echo "\r\n";
  }

Open in new window

0
Comment
Question by:Jake_D87
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37831245
Security restrictions prevent you from saving a file on someone's computer without their permission.  SAVE/OPEN is all you get.
0
 

Author Comment

by:Jake_D87
ID: 37831249
No i want it to save the csv into the same folder that the php file is in...
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37831305
Then you don't need the 'header' lines.  Those are for sending to a browser.  To save into a file on the server, you need to collect the data into a variable and then write it to the local file.  See the example on this page: http://us.php.net/manual/en/function.fwrite.php  This does require that you have 'write' permission in that folder.  On a web server, PHP running from a browser request usually doesn't so you may have to see about setting the permissions.
0
 

Author Comment

by:Jake_D87
ID: 37831406
One problem, when i run this with a cronjob it doesnt create the csv file... I know its running because there is an SQL script which is executing successfully. Any ideas?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37831539
Does your user have full permissions in the directory where you are trying to write it?  Or is it just the web server that has permissions there?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question