Vẫn còn 2 cách có thể được sử dụng để sao chép các tệp của bạn từ một máy chủ khác.
-Một là để xóa tệp .htaccess của bạn khỏi example.com hoặc cho phép truy cập vào tất cả các tệp [bằng cách sửa đổi tệp .htaccess của bạn]. -Access/đọc các tệp đó thông qua các URL tương ứng của chúng và lưu các tệp đó bằng các phương thức 'file_get_contents []' và 'file_put_contents []'. Nhưng cách tiếp cận này cũng sẽ làm cho tất cả các tập tin có thể truy cập được cho người khác.
$fileName = 'filename.extension';
$sourceFile = '//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]];
}
}
Hoặc bạn có thể tạo proxy/dịch vụ trên example.com để đọc một tệp cụ thể sau khi xác thực khóa thông qua hoặc kết hợp tên người dùng/mật khẩu [bất cứ điều gì theo yêu cầu của bạn].
//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.
}
Bạn có thể truy cập proxy/dịch vụ đó bằng URL '//example.com/myproxy.php?file=filename.extension&passkey=my-secret-key'.
Đôi khi bạn cần di chuyển/di chuyển các tệp sang máy chủ/lưu trữ khác và bạn/khách hàng của bạn chỉ có quyền truy cập FTP vào máy chủ. Và để tải xuống các tệp này và tải lại cho một máy chủ khác có thể mất nhiều thời gian bằng cách sử dụng máy khách FTP như Filezilla. FTP không có chức năng Zip - Unzip, vì vậy bạn cần tải lên từng cái một. Và máy chủ sang máy chủ chuyển nhanh hơn rất nhiều so với tải xuống và tải lên các tệp.
Bạn có thể sử dụng tập lệnh PHP đơn giản này để di chuyển các tệp từ máy chủ này sang máy chủ khác.
Lưu ý: Nó chỉ là một ví dụ đơn giản. Bạn cần xây dựng phương pháp Auth/Security của riêng bạn nếu cần.
1. Sử dụng sao chép PHP để di chuyển các tệp từ máy chủ sang máy chủ.
Bạn chỉ có thể tạo một tệp PHP trong máy chủ đích và tải tệp một lần trong trình duyệt của bạn. Ví dụ: bạn thêm mã này trong //destination-url/copy-files.php và trong copy-files.php, bạn thêm mã PHP này:
/** * Transfer Files Server to Server using PHP Copy * @link //shellcreeper.com/?p=1249 */ /* Source File URL */ $remote_file_url = '//origin-server-url/files.zip'; /* New file name and path for this 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! success to copy $file...\n"; }
2. Sử dụng PHP FTP để di chuyển các tệp từ máy chủ sang máy chủ
Đôi khi sử dụng php sao chép didn hoạt động nếu các tệp được bảo vệ bằng phương pháp này bằng cách nào đó [có thể bảo vệ hotlink?]. Tôi đã trải nghiệm rằng nếu nguồn đến từ Hostgator thì nó đã không hoạt động.
Nhưng chúng ta có thể sử dụng một phương pháp khác. Sử dụng FTP [tính bằng PHP] để thực hiện chuyển nhượng bằng mã:
/** * Transfer [Import] Files Server to Server using PHP FTP * @link //shellcreeper.com/?p=1249 */ /* Source File Name and Path */ $remote_file = 'files.zip'; /* FTP Account */ $ftp_host = 'your-ftp-host.com'; /* host */ $ftp_user_name = ''; /* username */ $ftp_user_pass = 'ftppassword'; /* password */ /* New file name and path for this file */ $local_file = 'files.zip'; /* Connect using basic FTP */ $connect_it = ftp_connect[ $ftp_host ]; /* Login to FTP */ $login_result = ftp_login[ $connect_it, $ftp_user_name, $ftp_user_pass ]; /* Download $remote_file and save to $local_file */ if [ ftp_get[ $connect_it, $local_file, $remote_file, FTP_BINARY ] ] { echo "WOOT! Successfully written to $local_file\n"; } else { echo "Doh! There was a problem\n"; } /* Close the connection */ ftp_close[ $connect_it ];
Sử dụng FTP Bạn có tính linh hoạt hơn, mã ở trên đang sử dụng & nbsp; ________ 7 để nhập các tệp từ máy chủ nguồn sang máy chủ đích. Nhưng chúng tôi cũng có thể sử dụng ftp_put để xuất các tệp [gửi tệp] từ máy chủ nguồn sang đích, sử dụng mã này:
/** * Transfer [Export] Files Server to Server using PHP FTP * @link //shellcreeper.com/?p=1249 */ /* Remote File Name and Path */ $remote_file = 'files.zip'; /* FTP Account [Remote Server] */ $ftp_host = 'your-ftp-host.com'; /* host */ $ftp_user_name = ''; /* username */ $ftp_user_pass = 'ftppassword'; /* password */ /* File and path to send to remote FTP server */ $local_file = 'files.zip'; /* Connect using basic FTP */ $connect_it = ftp_connect[ $ftp_host ]; /* Login to FTP */ $login_result = ftp_login[ $connect_it, $ftp_user_name, $ftp_user_pass ]; /* Send $local_file to FTP */ if [ ftp_put[ $connect_it, $remote_file, $local_file, FTP_BINARY ] ] { echo "WOOT! Successfully transfer $local_file\n"; } else { echo "Doh! There was a problem\n"; } /* Close the connection */ ftp_close[ $connect_it ];
Để làm cho nó dễ hiểu hơn:
- ________ 8 & nbsp; là kết nối qua FTP.
- ________ 9 & nbsp; là đăng nhập vào tài khoản FTP sau khi kết nối được thiết lập.
- ________ 10 & nbsp; là đóng kết nối sau khi chuyển hoàn thành [đăng xuất].
- ________ 7 & nbsp; là nhập/tải xuống/kéo tệp qua FTP.
- ________ 12 & nbsp; là xuất/gửi/đẩy tệp qua FTP.
Sau khi bạn nhập/xuất tệp, luôn xóa tệp PHP bạn sử dụng để thực hiện nhiệm vụ này để ngăn người khác sử dụng nó.
Các tệp zip và giải nén bằng PHP
Tất nhiên để làm cho việc chuyển dễ dàng hơn, chúng tôi cần nén các tệp trước khi di chuyển và giải nén nó sau khi chúng tôi chuyển đến đích.
Tệp zip bằng PHP
Bạn có thể zip tất cả các tệp trong thư mục bằng mã này:
/** * ZIP All content of current folder * @link //shellcreeper.com/?p=1249 */ /* ZIP File name and path */ $zip_file = 'files.zip'; /* Exclude Files */ $exclude_files = array[]; $exclude_files[] = realpath[ $zip_file ]; $exclude_files[] = realpath[ 'zip.php' ]; /* Path of current folder, need empty or null param for current folder */ $root_path = realpath[ '' ]; /* 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[];
Giải nén các tệp bằng PHP
Bạn có thể giải nén tệp vào cùng một thư mục bằng mã này:
/** * Unzip File in the same directory. * @link //stackoverflow.com/questions/8889025/unzip-a-file-with-php */ $file = 'file.zip'; $path = pathinfo[ realpath[ $file ], PATHINFO_DIRNAME ]; $zip = new ZipArchive; $res = $zip->open[$file]; if [$res === TRUE] { $zip->extractTo[ $path ]; $zip->close[]; echo "WOOT! $file extracted to $path"; } else { echo "Doh! I couldn't open $file"; }
Thay thế khác cho tệp zip / unzip
Trên thực tế, nếu sử dụng cPanel, bạn có thể dễ dàng tạo các tệp zip và giải nén bằng cách sử dụng trình quản lý tệp cPanel.
Tôi hy vọng hướng dẫn này hữu ích cho bạn, những người cần một cách đơn giản để di chuyển các tệp từ máy chủ này sang máy chủ khác.