Solved

PHP/MySQL to create CSV in remote folder

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

920 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

15 Experts available now in Live!

Get 1:1 Help Now