我尝试使用不同的php代码将文件(original.php)从服务器复制到另一个服务器。以下是我尝试使用的代码:
代码1: <?
$file = "http://mysiteurl.com/original.php";
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/test/copiedfile.php';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
?>
代码2: <?
$remotefile="original.php";
$folder="/test/";
$srcfile1 = fopen("$remotefile", "r");
$namefile=basename($remotefile);
if (!($fp1 = fopen($folder.$name,"w")));
while ($contents = fread( $srcfile1, 8192 )) {
fwrite( $fp1, $contents, strlen($contents) );
}
fclose($srcfile1);
fclose($fp1);
echo"FILE TRANSFERRED";
?>
在这两种情况下,original.php和复制的文件是不一样的。在复制的文件中php代码全部消失。
下面是一个original .php
的例子 <?php
... some php code
?>
<!DOCTYPE html >
<html>
<head>
... come html code
copyedfile .php是
<!DOCTYPE html >
<html>
<head>
... come html code
如何解决这个问题?
你不能。
服务器将解析您想要复制的phpfile,因此所有php代码都将消失。通过在url(或file_get_contents)上使用copy,您将实际要求服务器为您提供文件,就像您在浏览器中请求一样。
你应该很高兴这样做,否则人们很容易窃取代码并从中找到任何秘密。