Solved

PHP/MySQL to create CSV in remote folder

Posted on 2012-04-10
5
285 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 82

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 82

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 82

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Responsive Design Tools 6 38
WooCommerce Sort by Date 4 10
Wordpress syntax error 1 28
Strip leading 0 from a var 3 13
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 …
The viewer will learn how to count occurrences of each item in an array.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now