php - 在php中以编程方式将文件从一台服务器复制到另一台服务器

标签 php ftp-client

我将所有代码文件托管在一台服务器上,其域类似于 example.com我在那些 html 页面之一。我希望 example.com 的一些文件移动到另一台服务器上,其域类似于 example2.com .我已经通过互联网搜索,但我找不到任何好的解决方案。请告诉我这是否可能没有 FTP 客户端或没有我们手动上传文件的浏览器。或者有什么方法可以将 HTML 表单中的文件从一台服务器提交到另一台服务器,就像我们将编写的操作一样

<form action="http://example2.com/action_page.php">

任何帮助将非常感激。

最佳答案

还有两种可能的方法可以用来从另一台服务器复制文件。

- 一个是从 example.com 删除您的 .htaccess 文件或允许访问所有文件(通过修改您的 .htaccess 文件)。
- 通过它们各自的 URL 访问/读取这些文件,并使用“file_get_contents()”和“file_put_contents()”方法保存这些文件。但是这种方法也将使其他人可以访问所有文件。

            $fileName       = 'filename.extension';
            $sourceFile     = 'http://example.com/path-to-source-folder/' . $fileName;
            $targetLocation = dirname( __FILE__ ) . 'relative-path-destination-folder/' + $fileName;

            saveFileByUrl($sourceFile, $targetLocation);

            function saveFileByUrl ( $source, $destination ) {
                if (function_exists('curl_version')) {
                    $curl   = curl_init($fileName);
                    $fp     = fopen($destination, 'wb');
                    curl_setopt($ch, CURLOPT_FILE, $fp);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_exec($ch);
                    curl_close($ch);
                    fclose($fp);
                } else {
                    file_put_contents($destination, file_get_contents($source));
                }
            }

或者,您可以在 example.com 上创建代理/服务以在验证密码或用户名/密码组合(根据您的要求进行任何操作)后读取特定文件。
            //In myproxy.php
            extract($_REQUEST);
            if (!empty($passkey) && paskey == 'my-secret-key') {
                if (!empty($file) && file_exists($file)) {
                    if (ob_get_length()) {
                        ob_end_clean();
                    }
                    header("Pragma: public");
                    header( "Expires: 0");
                    header( "Cache-Control: must-revalidate, post-check=0, pre-check=0");
                    header( 'Content-Type: ' . mime_content_type($file) );
                    header( "Content-Description: File Transfer");
                    header( 'Content-Disposition: attachment; filename="' . basename( $file ) . '"' );
                    header( "Content-Transfer-Encoding: binary" );
                    header( 'Accept-Ranges: bytes' );
                    header( "Content-Length: " . filesize( $file ) );
                    readfile( $file );
                    exit;
                } else {
                    // File not found 
                }
            } else {
                //  You are not authorised to access this file.
            }

您可以通过 url ' http://example.com/myproxy.php?file=filename.extension&passkey=my-secret-key 访问该代理/服务'。

关于php - 在php中以编程方式将文件从一台服务器复制到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656390/

相关文章:

linux - 在没有提示的情况下使用 FTP 的 mget

linux - 在 FileZilla 中以 root 身份运行 Atom 编辑器以查看/编辑文件

java - setDataTimeout 何时被调用?

php - shorthand 方法可以应用于foreach吗?

php - 使用 PHP 实现 PNG 透明度

php - "Load data local infile"命令不允许

php - FTP 连接太多,无法接受更多

php - 未知 curl 错误

php - MySQL 和 PHP 错误 : Column count doesn't match value count at row 1

linux - curlftpfs 错误地复制文件/目录名