PHP - 将CSV导出到FTP而不是在浏览器中下载

时间:2022-08-27 18:16:45

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:

我已经尝试了几个小时现在无济于事。我已使用以下命令在浏览器中成功输出我的CSV作为下载:

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.

但是我希望输出现在转到FTP服务器,我已经将连接和临时文件部分排序,但似乎无法编辑文件以输出我的结果。

// 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.

上面是获取查询数据并将其放入CSV的部分,我尝试在循环中合并fput和fputcsv以将行写入文件。

//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.

因此,回显输出工作绝对正常 - 我想要的是将回波数据写入CSV文件并上传到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?

所以fp引起了问题......是在开放阶段还是在编写csv_export?

Thanks in advance.

提前致谢。

5 个解决方案

#1


0  

Try this

尝试这个

//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);

Your current code is creating an empty file, uploading it, and then creating a separate file. Even if you get it working on a local server, it is an unnecessary step.

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

#2


0  

Try this, not tested:

试试这个,没试过:

$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);

#3


0  

In phpinfo(); you can look for Registered PHP Streams and just use this in most of any cases:

在phpinfo();您可以查找已注册的PHP Streams并在大多数情况下使用它:

file_put_contents('ftp://user:password@server/your/directory/file.csv', $csvData);

#4


0  

ftp_fput is a right way here.

ftp_fput是一个正确的方法。

But you have to prepare FTP connection and authenticate first. Assuming you have $csv_export string in memory:

但您必须先准备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);

#5


0  

You're receiving that error because fopen is returning false, which indicates that it was unable to open the file.

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

If I'm understanding your code, you're uploading abandoned.csv (which presumably exists) to the ftp after opening it in $temp. Is $temp pointing at your local copy of abandon.csv? Because if so, you should close the file stream pointing to it before opening a second one with fopen.

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

If that's not the issue, then abandoned.csv may not actually exist, in which case you may just be trying to fopen a missing file.

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

#1


0  

Try this

尝试这个

//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);

Your current code is creating an empty file, uploading it, and then creating a separate file. Even if you get it working on a local server, it is an unnecessary step.

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

#2


0  

Try this, not tested:

试试这个,没试过:

$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);

#3


0  

In phpinfo(); you can look for Registered PHP Streams and just use this in most of any cases:

在phpinfo();您可以查找已注册的PHP Streams并在大多数情况下使用它:

file_put_contents('ftp://user:password@server/your/directory/file.csv', $csvData);

#4


0  

ftp_fput is a right way here.

ftp_fput是一个正确的方法。

But you have to prepare FTP connection and authenticate first. Assuming you have $csv_export string in memory:

但您必须先准备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);

#5


0  

You're receiving that error because fopen is returning false, which indicates that it was unable to open the file.

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

If I'm understanding your code, you're uploading abandoned.csv (which presumably exists) to the ftp after opening it in $temp. Is $temp pointing at your local copy of abandon.csv? Because if so, you should close the file stream pointing to it before opening a second one with fopen.

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

If that's not the issue, then abandoned.csv may not actually exist, in which case you may just be trying to fopen a missing file.

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