在 PHP 中创建文件并将其上传到 FTP 服务器,而不在本地保存
我在连接我正在工作的两个不同进程时遇到问题.我的任务是从数据库中提取数据,从数据中创建文件,然后将其上传到 FTP 服务器.
到目前为止,我已经使用此代码创建和下载了文件,$out
是一个包含完整文本文件的字符串:
if ($output == 'file'){header("Cache-Control: must-revalidate, post-check=0, pre-check=0");header("内容长度:".strlen($out));header("内容类型:应用程序/txt");header("内容配置:附件;文件名=".$file_name);回声($out);}
这适用于我只想在浏览器中运行脚本并下载文件,但我希望将其发送到 FTP 服务器.
我知道我与 FTP 服务器的连接工作正常,并且我正确导航到正确的目录,并且我已经从磁盘获取文件并使用 ftp_put() 将它们放在 FTP 上
,但我希望将 $out
直接作为文件写入,并在 FTP 服务器上以 $filename
作为其名称.我可能误读了一些东西,但是当我尝试 ftp_put
和 ftp_fput
时,似乎他们想要的是文件位置,而不是文件流.我可以考虑其他功能吗?
我在这里回答而不是评论,因为评论忽略了代码格式.
你可以试试:
<上一页><代码>$fp = fopen('php://temp', 'r+');fwrite($fp, $out);倒带($fp);ftp_fput($ftp_conn, $remote_file_name, $fp, FTP_ASCII);代码>这将创建一个临时流而不实际将其写入磁盘.我不知道还有什么办法
I'm having a problem connecting two different processes that I have working. I've been tasked with pulling data out of a database, creating a file from the data, and then uploading it to an FTP server.
So far, I have the file being created and downloadable using this code, $out
being a string that contains the complete text file:
if ($output == 'file')
{
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: ".strlen($out));
header("Content-type: application/txt");
header("Content-Disposition: attachment; filename=".$file_name);
echo($out);
}
This works when I want to just run the script in a browser and download the file, but I'm looking to instead send it to an FTP server.
I know my connection to the FTP server is working just fine, and I'm correctly navigating to the correct directory, and I've taken files from disk and put them on the FTP using ftp_put()
, but I'm looking to take $out
and write it directly as a file with $filename
as its name on the FTP server. I may be misreading things, but when I tried ftp_put
and ftp_fput
, it seemed that they want file locations, not file streams. Is there a different function I could consider?
I've answered here instead of the comments since comments ignore code formatting.
you could try:
$fp = fopen('php://temp', 'r+');
fwrite($fp, $out);
rewind($fp);
ftp_fput($ftp_conn, $remote_file_name, $fp, FTP_ASCII);
this will create a temporary stream without actually writing it to disk. I don't know any other way
相关文章