简体   繁体   English

PHP-使用FTP合并文件

[英]PHP - Merging files with FTP

So, I'm trying to copy a file from my local server to a remote server with FTP. 因此,我正在尝试使用FTP将文件从本地服务器复制到远程服务器。 The problem is, I need to do this in chunks. 问题是,我需要分批执行此操作。

Thus far, based on my research, it looks like it will probably be easiest to do this by making two files on my local server: the file to be copied, and a small, temporary file that holds the current chunk. 到目前为止,根据我的研究,通过在本地服务器上创建两个文件似乎是最容易做到的:要复制的文件和一个保存当前块的临时文件。 Then, I should simply merge that chunk with the remote file. 然后,我应该简单地将该块与远程文件合并。

The problem is, I'm having trouble appending files. 问题是,我无法附加文件。 I can't figure out how the ftp:// protocol works, and I can't find a comprehensive explanation on how to do it with cURL. 我无法确定ftp://协议的工作方式,也无法找到有关如何使用cURL进行全面解释的方法。 The closest I've found is this , but I couldn't get it working. 我发现的最接近的是this ,但是我无法正常工作。

Below is what I've written up so far. 以下是我到目前为止所写的内容。 I've commented it so you can just skim through it to get the idea, and see where I'm stuck--the code isn't complicated. 我已经对其进行了评论,因此您可以浏览一下以了解它的想法,并了解我的问题所在-代码并不复杂。 What do you recommend I do? 你推荐我做什么? How do I append a file on my local server a file on a remote server with FTP? 如何使用FTP将本地服务器上的文件附加到远程服务器上的文件?

<?php

// FTP credentials
$server = HIDDEN;
$username = HIDDEN;
$password = HIDDEN;

// Connect to FTP
$connection = ftp_connect($server) or die("Failed to connect to <b>$server</b>.");

// Login to FTP
if (!@ftp_login($connection, $username, $password))
{
    echo "Failed to login to <b>$server</b> as <b>$username</b>.";
}

// Destination file (where the copied file should go)
$destination = 'final.txt';

// The file on my server that we're copying (in chunks) to $destination.
$read = 'readme.txt';

// Current chunk of $read.
$temp = 'temp.tmp';

// If the file we're trying to copy exists...
if (file_exists($read))
{
    // Set a chunk size (this is tiny, but I'm testing
    // with tiny files just to make sure it works)
    $chunk_size = 4;

    // For reading through the file we want to copy to the FTP server.
    $read_handle = fopen($read, 'r');

    // For writing the chunk to its own file.
    $temp_handle = fopen($temp, 'w+');

    // Loop through $read until we reach the end of the file.
    while (!feof($read_handle))
    {
        // Read a chunk of the file we're copying.
        $chunk = fread($read_handle, $chunk_size);

        // Write that chunk to its own file.
        fwrite($temp_handle, $chunk);

        ////////////////////////////////////////////
        ////                                    ////
        ////       NOW WHAT?? HOW DO I          ////
        ////       WRITE / APPEND THAT          ////
        ////       CHUNK TO $destination?       ////
        ////                                    ////
        ////////////////////////////////////////////
    }
}

fclose($read_handle);
fclose($temp_handle);
ftp_close($ftp_connect);
?>

First off i don't think you need to create the tmp file. 首先,我认为您不需要创建tmp文件。 You can append to the destination file either using "append mode" ( a ) as defined in the manual or use file_put_contents with the FILE_APPEND flag. 您可以使用手册中定义的“追加模式”( a )或使用带有FILE_APPEND标志的file_put_contents追加到目标文件。

file_put_contents does takes a filename as parameter not a file handle so it basically does fopen for you which means that if you write frequently it will fopen frequently which is less optimal compared to using the file handle ad fwrite . file_put_contents确实将文件名作为参数而不是文件句柄,因此它基本上会为您fopen ,这意味着,如果您频繁写入,它将经常fopen ,这与使用文件句柄ad fwrite相比不是那么理想。

<?php
// The URI of the remote file to be written to
$write = 'ftp://username1:password1@domain1.com/path/to/writeme.txt';

// The URI of the remote file to be read
$read = 'ftp://username2:password2@domain2.com/path/to/readme.txt';

if (file_exists($read)) // this will work over ftp too
{
    $chunk_size = 4;
    $read_handle = fopen($read, 'r');
    $write_handle = fopen($write, 'a');

    while (!feof($read_handle))
    {
        $chunk = fread($read_handle, $chunk_size);
        fwrite($write_handle, $chunk);
    }
}

fclose($read_handle);
fclose($write_handle);
?>

As a side note: PHP has stream wrappers that make ftp access a breeze without using the ftp functions themselves. 附带说明:PHP具有流包装器,可以轻松进行ftp访问,而无需使用ftp函数本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM