?
Solved

PHP/MySQL to create CSV in remote folder

Posted on 2012-04-10
5
Medium Priority
?
296 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 2000 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

801 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