copy file from one server to another programmatically in php

痴心易碎 提交于 2019-12-10 10:45:02

问题


I've hosted my all code files on one server whose domain is like example.com and I'm at one of those html pages. I want some files of example.com to move on another server whose domain is like example2.com. I've searched through Internet but I couldn't find any good solution. Please tell me is this possible without FTP client or without browser where we upload files manually. Or is there any way to submit file from HTML form from one server to another like if on action we'll write

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

Any help would be much appreciated.


回答1:


There are still 2 possible ways which can used to copy your files from another server.

-One is to remove your .htaccess file from example.com or allow access to all files(by modifying your .htaccess file). -Access/Read those files via their respective URLs, and save those files using 'file_get_contents()' and 'file_put_contents()' methods. But this approach will made all files accessible to other people too.

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

Or you can create a proxy/service on example.com to read a specific file after validating a pass key or username/password combination(whatever as per your requirement).

            //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.
            }

you can access that proxy/service by url 'http://example.com/myproxy.php?file=filename.extension&passkey=my-secret-key'.




回答2:


If you set the contents of this file: example2.com/action_page.php to:

<?php
$tobecopied = 'http://www.example.com/index.html';
$target = $_SERVER['DOCUMENT_ROOT'] . '/contentsofexample/index.html';

if (copy($tobecopied, $target)) {
    //File copied successfully
}else{
    //File could not be copied
}
?>

and run it, through command line or cron (as you have written a php related question, yet banned use of browsers!) it should copy the contents of example.com/index.html to a directory of your site (domain: example2.com) called contentsofexample.

N.B. If you wanted this to copy the whole website you should place it in a for loop



来源:https://stackoverflow.com/questions/27656390/copy-file-from-one-server-to-another-programmatically-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!