Send a variable's content to a file through FTP
It's possible using wrappers:
ftp_put($conn_id, $destination_file, 'data://text/plain;base64,' . base64_encode($file_contents), FTP_BINARY);
Transfer files between two remote FTP servers in PHP
Both ftp_get
and ftp_put
can operate with files only, not folders.
Use ftp_get
to download a file from the first server to a local temporary folder/file. And then use ftp_put
to upload the temporary file to the second server.
If you want to avoid using a temporary file, you can download the file to memory using ftp_fget
and re-upload to the second server using ftp_fput
.
- PHP: How do I read a .txt file from FTP server into a variable?
- Transfer in-memory data to FTP server without using intermediate file
Updating CSV file in FTP server in PHP
You file handler $file
points at the end of the file as you write in it. There is nothing left to write via ftp_fput
.
You can reset your file pointer at the beginning of the file with rewind($file);
before writing in the FTP : rewind documentation
Generate CSV file on an external FTP server in PHP
If you have ftp://
URL wrappers enabled, just open the file directly on FTP server:
$fp = fopen('ftp://username:password@ftp.example.com/path/to/somefile', 'w');
If you do not have the wrapers enabled, see:
Creating and uploading a file in PHP to an FTP server without saving locally
How to upload a file from a MemoryStream to an FTP server
Either use FtpWebRequest
, as you can see in Upload a streamable in-memory document (.docx) to FTP with C#?:
WebRequest request =
WebRequest.Create("ftp://ftp.example.com/remote/path/filename.xml");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
memoryStream.CopyTo(ftpStream);
}
or use WebClient.OpenWrite
(as you can also see in the answer by @Neptune):
using (var webClient = new WebClient())
{
const string url = "ftp://ftp.example.com/remote/path/filename.xml";
using (Stream uploadStream = client.OpenWrite(url))
{
memoryStream.CopyTo(uploadStream);
}
}
Equivalently, your existing FileStream
code can be simplified to:
using (var outputFile = File.Create($@"{serverPath}\{filename}.xml"))
{
stream.CopyTo(outputFile);
}
Though obviously, even better would be to avoid the intermediate MemoryStream
and write the XML directly to FileStream
and WebRequest.GetRequestStream
(using their common Stream
interface).
Can you append lines to a remote file using ftp_put() or something similar?
Curl support appending for FTP:
curl_setopt($ch, CURLOPT_FTPAPPEND, TRUE ); // APPEND FLAG
This might be what you're looking for. Are you familiar with curl?
The other option is to use ftp://
/ ftps://
streams, since PHP 5 they allow appending. See ftp://; ftps:// Docs. Might be easier to access.
Upload a streamable in-memory document (.docx) to FTP with C#?
Write the document directly to the request stream. There's no point using an intermediate MemoryStream
. And StreamReader
/StreamWriter
are for working with text files, while a .docx
is a binary file format, so do not use those either.
WebRequest request =
WebRequest.Create("ftp://ftp.example.com/remote/path/document.docx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
document2.SaveToStream(ftpStream, Spire.Doc.FileFormat.Docx);
}
Or use WebClient.OpenWrite
:
using (var webClient = new WebClient())
{
const string url = "ftp://ftp.example.com/remote/path/document.docx";
using (Stream uploadStream = client.OpenWrite(url))
{
document2.SaveToStream(uploadStream, Spire.Doc.FileFormat.Docx);
}
}
You will only need an intermediate MemoryStream
, if the Spire library requires a seekable stream, what the Stream
returned by FtpWebRequest.GetRequestStream
is not. I cannot test that.
If that's the case, use:
MemoryStream memoryStream = new MemoryStream();
document2.SaveToStream(memoryStream, Spire.Doc.FileFormat.Docx);
memoryStream.Seek(0, SeekOrigin.Begin);
WebRequest request =
WebRequest.Create("ftp://ftp.example.com/remote/path/document.docx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
memoryStream.CopyTo(ftpStream);
}
Or again, you can use WebClient.OpenWrite
as in the previous example.
See also a similar question Zip a directory and upload to FTP server without saving the .zip file locally in C#.
Related Topics
Laravel: Validate an Integer Field That Needs to Be Greater Than Another
Jquery Ajax Form Using Mail() PHP Script Sends Email, But Post Data from HTML Form Is Undefined
Sqlstate[Hy000] [1045] Access Denied for User 'Username'@'Localhost' Using Cakephp
Read a File Backwards Line by Line Using Fseek
How to Override Trait Function and Call It from the Overridden Function
How to Add New Column to MySQL Table
How to Write JavaScript Code Inside PHP
PHP Curl: I Need a Simple Post Request and Retrival of Page Example
Call to Undefined Function _() Error - PHPmyadmin
What Is a Unix Timestamp and Why Use It
Post and Get at the Same Time in PHP
Sqlsrv_Num_Rows Not Returning Any Value
Traits in PHP - Any Real World Examples/Best Practices