Solved

PHP/MySQL to create CSV in remote folder

Posted on 2012-04-10
5
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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