Php 不要从数据库下载csv导出,而是通过ftp写入服务器

Php 不要从数据库下载csv导出,而是通过ftp写入服务器,php,Php,好的,我有一个脚本,它运行并从我的数据库中生成一个csv文件,当它从浏览器中运行时,它会下载,很好 但我想将文件从我的服务器发布到另一台服务器 我一直试图用这段代码来实现它,但它似乎不起作用,没有编写任何文件。即使FTP帐户详细信息也错误,登录确认也会返回 // header("Content-type: application/octet-stream"); // header("Content-Disposition: attachment; filename=sailings.txt");

好的,我有一个脚本,它运行并从我的数据库中生成一个csv文件,当它从浏览器中运行时,它会下载,很好

但我想将文件从我的服务器发布到另一台服务器

我一直试图用这段代码来实现它,但它似乎不起作用,没有编写任何文件。即使FTP帐户详细信息也错误,登录确认也会返回

// header("Content-type: application/octet-stream");
// header("Content-Disposition: attachment; filename=sailings.txt");
// header("Pragma: no-cache");
// header("Expires: 0");
// print "$header\n$data";

//Connect to the FTP server
$ftpstream = ftp_connect('ftp server address');

//Login to the FTP server
$login = ftp_login($ftpstream, 'user', 'password');
if($login) {
echo "logged in ok";
//We are now connected to FTP server.
//Create a temporary file
$temp = tmpfile();
fwrite($temp, $header."\n");
fwrite($temp, $data);
fseek($temp, 0);
echo fread($temp, 0);

//Upload the temporary file to server
ftp_fput($ftpstream, '/sailings.txt', $temp, FTP_ASCII);

//Make the file writable only to owner
ftp_site($ftpstream,"CHMOD 0644 /sailings.txt");
}

//Ftp session end
fclose($temp);
ftp_close($ftpstream);
有谁能给我建议吗

谢谢


Rich:)

您的问题是tmpfile函数返回文件的句柄,ftp\u put函数需要接收要上载的文件的路径+名称。从这个意义上讲,这两条指令的操作不匹配

要解决这个问题:

$f = tempnam("/tmp", "FOO");
$temp = = fopen($f, "w");
fwrite($temp, $header."\n");
fwrite($temp, $data);
fseek($temp, 0);
echo fread($temp, 0);

//Upload the temporary file to server
ftp_put($ftpstream, '/sailings.txt', $f, FTP_ASCII);

// The rest is the same as you have
试试这个解决方案,祝你好运


编辑:感谢Felipe的评论。您正在正确使用ftp\u fput。尽管如此,如果您的问题仍然存在,并且仍然受到阻碍,您可以使用给定的策略来查看发生了什么。

ftp\u fput(resource$ftp\u stream,string$remote\u file,**resource$handle**,int$mode[,int$startpos=0])
来自PHP5Manual@FelipeAlamedaA你的消息来源是什么?我读到:bool ftp_put(resource$ftp_stream,string$remote_file,string$local_file,int$mode[,int$startpos=0]),其中$local_file是:“本地文件路径”。我们讨论的是不同的功能:ftp_put和ftp_fput。操作码包括最后一个。这可能发生在任何人身上。没什么好担心的,也许你应该在你的答案被否决之前删除它。也就是说,如果没有新的想法。你确定这确实有联系吗?继续之前,请尝试测试$ftpstream是否为false。