使用PHP创建并上传文件到FTP服务器而不保存在本地 [英] Creating and uploading a file in PHP to an FTP server without saving locally

查看:478
本文介绍了使用PHP创建并上传文件到FTP服务器而不保存在本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接两个不同进程的问题,我正在工作。我的任务是将数据从数据库中提取出来,从数据创建一个文件,然后将其上传到FTP服务器。



到目前为止,我已经使用此代码创建和下载文件, $ out 是一个字符串,它包含完整的文本文件:

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

当我想在浏览器中运行脚本并下载文件,但我希望将它发送到FTP服务器。



我知道我与FTP服务器的连接工作正常,我正确导航到正确的目录,我从磁盘获取文件并使用 ftp_put()将它们放在FTP上,但是我期望将 $ out ,并将其直接写入为具有 $ filename 的文件作为FTP服务器上的名称。我可能会误读东西,但是当我试过 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

这篇关于使用PHP创建并上传文件到FTP服务器而不保存在本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆