4

I have been trying for a few hours now to no avail. I have successfully output my CSV as a download in the browser using:

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=".$csv_filename."");

However I want that output to now go to an FTP server instead, I have got the connection and temp file part sorted but cannot seem to edit the file to output my results.

// create empty variable to be filled with export data
$csv_export = '';

for($i = 0; $i < $field; $i++) {
$csv_export.= mysql_field_name($query,$i).',';
}
// newline (seems to work both on Linux & Windows servers)
$csv_export.= '
';

// loop through database query and fill export variable
while($row = mysql_fetch_array($query)) {
// create line with field values
for($i = 0; $i < $field; $i++) {
$csv_export.= '"'.$row[mysql_field_name($query,$i)].'",';
}   
$csv_export.= '
';  
}

Above is the part that gets the query data and puts it into the CSV, I have tried incorporating both fputs and fputcsv in the loops to write the rows to the file.

//Upload the temporary file to server
ftp_fput($ftpstream, '/httpdocs/.../abandoned.csv', $temp, FTP_ASCII);

$fp = fopen('var/www/vhosts/.../abandoned.csv', 'w');
fputs($fp, '$csv_export');
fclose($fp);
echo($csv_export);

So, echoing the output works absolutely fine - all I want is that echo'd data written into a CSV file and uploaded to the FTP.

The error I've been given is:

Array ( [type] => 2 [message] => fclose() expects parameter 1 to be resource, 
boolean given

So the fp is causing the problem... is it at the open stage or at the writing of csv_export?

Thanks in advance.

4

5 回答 5

2

尝试这个

//Write the contents to the temp file
fputs($temp, $csv_export);

//upload the temp file
ftp_fput($ftpstream, '/httpdocs/.../abandoned.csv', $temp, FTP_ASCII);

//close the temp file
fclose($temp);

您当前的代码正在创建一个空文件,上传它,然后创建一个单独的文件。即使你让它在本地服务器上工作,这也是一个不必要的步骤。

于 2013-07-01T10:39:28.987 回答
0

试试这个,未经测试:

$filename = 'abandoned.csv';
$content  = $csv_export;

$host     = 'yourhost';
$user     = 'youruser'; 
$password = 'yourpass';

//open connection to your ftp server
$stream = stream_context_create(array('ftp' => array('overwrite' => true)));

//authenticate to your ftp server, normal uri syntax
$uri = sprintf('ftp://%s:%s@%s/%s', $user, $password, $host, $filename);
file_put_contents($uri, $content, 0, $stream);
于 2013-07-01T09:42:48.023 回答
0

您收到该错误是因为 fopen 返回 false,这表明它无法打开文件。

如果我理解您的代码,您将在 $temp 中打开它后将被放弃的.csv(可能存在)上传到 ftp。$temp 是否指向您的本地放弃.csv 副本?因为如果是这样,您应该在使用 fopen 打开第二个文件流之前关闭指向它的文件流。

如果这不是问题,那么被放弃的.csv 可能实际上并不存在,在这种情况下,您可能只是试图打开丢失的文件。

于 2013-07-01T09:49:35.253 回答
0

在大多数情况下,phpinfo();您都可以查找并使用它:Registered PHP Streams

file_put_contents('ftp://user:password@server/your/directory/file.csv', $csvData);
于 2013-07-01T09:47:00.040 回答
0

ftp_fput在这里是正确的方法。

但是您必须先准备 FTP 连接并进行身份验证。假设您$csv_export在内存中有字符串:

// connect
$ftp_conn = ftp_connect('ftp.yourserver.net');

// authenticate
$login_result = ftp_login($conn_id, 'ftp_username', 'password');

// prepare output stream
$stream = fopen('data://text/plain,' . $csv_export, 'r');

// try to upload
if (ftp_fput($ftp_conn, '/httpdocs/.../abandoned.csv', $stream, FTP_ASCII)) {
    echo "Successfully uploaded\n";
} else {
    echo "There was a problem while uploading\n";
}

// close the connection and the file handler
ftp_close($ftp_conn);
fclose($stream);
于 2013-07-01T09:47:19.537 回答