在php中以编程方式将文件从一台服务器复制到另一台服务器
我在一台服务器上托管了我所有的代码文件,它的域类似于在php中以编程方式将文件从一台服务器复制到另一台服务器,php,ftp-client,Php,Ftp Client,我在一台服务器上托管了我所有的代码文件,它的域类似于example.com,我现在就在其中一个html页面上。我希望example.com的一些文件移动到另一台服务器上,该服务器的域类似于example2.com。我在网上搜索过,但找不到好的解决办法。请告诉我,这是没有FTP客户端或没有浏览器,我们手动上传文件可能。或者有没有什么方法可以像我们要写的那样,从一个服务器向另一个服务器提交HTML表单中的文件 <form action="http://example2.com/action_p
example.com
,我现在就在其中一个html页面上。我希望example.com的一些文件移动到另一台服务器上,该服务器的域类似于example2.com
。我在网上搜索过,但找不到好的解决办法。请告诉我,这是没有FTP客户端或没有浏览器,我们手动上传文件可能。或者有没有什么方法可以像我们要写的那样,从一个服务器向另一个服务器提交HTML表单中的文件
<form action="http://example2.com/action_page.php">
如果您将此文件的内容:example2.com/action\u page.php设置为:
<?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
}
?>
并通过命令行或cron运行它(正如您编写了一个与php相关的问题,但禁止使用浏览器!),它应该将example.com/index.html的内容复制到名为contentsofexample的站点目录(域:example2.com)中
N.B.如果你想复制整个网站,你应该把它放在for循环中还有两种可能的方法可以用来从另一台服务器复制文件 -一种是从example.com中删除.htaccess文件,或者允许访问所有文件(通过修改.htaccess文件)。 -通过各自的URL访问/读取这些文件,并使用“file\u get\u contents()”和“file\u put\u 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上创建一个代理/服务,以便在验证密钥或用户名/密码组合后读取特定文件(根据您的要求)
您可以通过url“”访问该代理/服务。您也可以使用zip方法在服务器之间交换文件。 您拥有这两台服务器,因此不应出现安全问题 在托管所需文件或文件夹的服务器上,创建一个php脚本并为其创建一个cron作业。示例代码如下:
<?php
/**
* ZIP All content of current folder
/* ZIP File name and path */
$zip_file = 'myfiles.zip';
/* Exclude Files */
$exclude_files = array();
$exclude_files[] = realpath( $zip_file );
$exclude_files[] = realpath( 'somerandomzip.php' );
/* Path of current folder, need empty or null param for current folder */
$root_path = realpath( '' ); //or $root_path = realpath( 'folder_name' ); if the file(s) are in a particular folder residing in the root
/* Initialize archive object */
$zip = new ZipArchive;
$zip_open = $zip->open( $zip_file, ZipArchive::CREATE );
/* Create recursive files list */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $root_path ),
RecursiveIteratorIterator::LEAVES_ONLY
);
/* For each files, get each path and add it in zip */
if( !empty( $files ) ){
foreach( $files as $name => $file ) {
/* get path of the file */
$file_path = $file->getRealPath();
/* only if it's a file and not directory, and not excluded. */
if( !is_dir( $file_path ) && !in_array( $file_path, $exclude_files ) ){
/* get relative path */
$file_relative_path = str_replace( $root_path, '', $file_path );
/* Add file to zip archive */
$zip_addfile = $zip->addFile( $file_path, $file_relative_path );
}
}
}
/* Create ZIP after closing the object. */
$zip_close = $zip->close();
?>
瞧
FTP有什么问题?@Dai我想自动完成。不是手动的:)
<?php
/**
* ZIP All content of current folder
/* ZIP File name and path */
$zip_file = 'myfiles.zip';
/* Exclude Files */
$exclude_files = array();
$exclude_files[] = realpath( $zip_file );
$exclude_files[] = realpath( 'somerandomzip.php' );
/* Path of current folder, need empty or null param for current folder */
$root_path = realpath( '' ); //or $root_path = realpath( 'folder_name' ); if the file(s) are in a particular folder residing in the root
/* Initialize archive object */
$zip = new ZipArchive;
$zip_open = $zip->open( $zip_file, ZipArchive::CREATE );
/* Create recursive files list */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $root_path ),
RecursiveIteratorIterator::LEAVES_ONLY
);
/* For each files, get each path and add it in zip */
if( !empty( $files ) ){
foreach( $files as $name => $file ) {
/* get path of the file */
$file_path = $file->getRealPath();
/* only if it's a file and not directory, and not excluded. */
if( !is_dir( $file_path ) && !in_array( $file_path, $exclude_files ) ){
/* get relative path */
$file_relative_path = str_replace( $root_path, '', $file_path );
/* Add file to zip archive */
$zip_addfile = $zip->addFile( $file_path, $file_relative_path );
}
}
}
/* Create ZIP after closing the object. */
$zip_close = $zip->close();
?>
/**
* Transfer Files Server to Server using PHP Copy
*
*/ /* Source File URL */
$remote_file_url = 'url_of_the_zipped';
/* New file name and path for this file */
$local_file ='myfiles.zip';
/* Copy the file from source url to server */
$copy = copy($remote_file_url, $local_file );
/* Add notice for success/failure */
if( !$copy ) {
echo "Failed to copy $file...\n"; }
else{
echo "Copied $file successfully...\n"; }
$file = 'myfiles.zip';
$path = pathinfo( realpath( $file ), PATHINFO_DIRNAME );
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo( $path );
$zip->close();
echo "$file extracted to $path"; }
else {
echo "Couldn't open $file";
}
?>
<?php
/* Source File URL */
$remote_file_url = 'http://origin-server-url/files.zip';
/* New file name and path for this new file */
$local_file = 'files.zip';
/* Copy the file from source url to server */
$copy = copy( $remote_file_url, $local_file );
/* Add notice for success/failure */
if( !$copy ) {
echo "Doh! failed to copy $file...\n";
}
else{
echo "WOOT! Thanks Munshi and OBOYOB! success to copy $file...\n";
}
?>