copy file from one server to another programmatically in php

Tabassum Ali picture Tabassum Ali · Dec 26, 2014 · Viewed 9.1k times · Source

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.

Answer

JBithell picture JBithell · Dec 26, 2014

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