仍有 2 种可能的方式可用于从另一台服务器复制文件。
-一种是从 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' 访问该代理/服务。