Hướng dẫn php move file to another server - php di chuyển tệp sang máy chủ khác

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 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     = '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));
                }
            }

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 'http://example.com/myproxy.php?file=filename.extension&passkey=my-secret-key'.

Chào mừng bạn đến với một hướng dẫn nhanh về cách di chuyển các tập tin trong PHP. Cần di chuyển một tệp từ một thư mục sang một thư mục khác?

Di chuyển các tệp trong PHP đơn giản như sử dụng một hàm duy nhất - rename("SOURCE.FILE", "FOLDER/TARGET.FILE"). Có, không có chức năng tệp di chuyển trong PHP và chúng tôi thực sự đổi tên một tệp thành một thư mục khác.

Điều đó sẽ bao gồm những điều cơ bản, nhưng mọi thứ khác nhau khi nói đến tập tin nâng cao di chuyển trực tuyến - đọc tiếp theo để biết thêm các ví dụ!

Tôi đã bao gồm một tệp zip với tất cả các mã nguồn ví dụ khi bắt đầu hướng dẫn này, vì vậy bạn không phải sao chép mọi thứ, hoặc nếu bạn chỉ muốn đi thẳng vào.

Slide nhanh

Hướng dẫn php move file to another server - php di chuyển tệp sang máy chủ khác

MỤC LỤC

Tải xuống & ghi chú

Hướng dẫn php move file to another server - php di chuyển tệp sang máy chủ khác

Thứ nhất, đây là liên kết tải xuống đến mã ví dụ như đã hứa.

Ghi chú nhanh

Nếu bạn phát hiện ra một lỗi, hãy bình luận bên dưới. Tôi cũng cố gắng trả lời các câu hỏi ngắn, nhưng đó là một người so với toàn bộ thế giới, nếu bạn cần câu trả lời khẩn cấp, vui lòng kiểm tra danh sách các trang web của tôi để nhận trợ giúp lập trình.

Mã hóa ví dụ Tải xuống

Nhấn vào đây để tải xuống mã nguồn, tôi đã phát hành nó theo giấy phép MIT, vì vậy hãy thoải mái xây dựng trên đó hoặc sử dụng nó trong dự án của riêng bạn.

Được rồi, bây giờ chúng ta hãy đi vào các cách khác nhau và ví dụ về cách di chuyển các tệp trong PHP.

1) Tệp đổi tên cơ bản

1-rename.php

Như trong phần giới thiệu, việc thay đổi tên tệp cũng dễ dàng như sử dụng hàm

            //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.
            }
0. Nhưng xin lưu ý rằng nếu có
            //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.
            }
1 hiện có, nó sẽ bị ghi đè mà không có bất kỳ cảnh báo nào.

2) Tệp di chuyển cơ bản

2-move.php

Như trong phần giới thiệu một lần nữa, không có chức năng

            //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.
            }
2 trong PHP - chúng tôi thực sự chỉ sử dụng
            //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.
            }
0 để di chuyển một tệp từ thư mục này sang thư mục khác.

3) Di chuyển mà không ghi đè

3-safe-move.php

Để ngăn chặn việc ghi đè lên các tệp di chuyển, chúng tôi có thể thực hiện kiểm tra

            //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.
            }
4 đơn giản - sau đó, bạn quyết định. Hoặc là don di chuyển tệp hoặc chọn một tên tệp khác.

4) Di chuyển các tệp của một số tiện ích mở rộng nhất định

4-move.ext.php

0) { foreach ($files as $f) {
    $moveTo = $dest . basename($f);
    echo rename($f, $moveTo) 
      ? "$f moved to $moveTo\r\n"
      : "Error moving $f to $moveTo\r\n";
  }}
}
movetype("jpg,png,gif", "d:/from/", "d:/to/");

Chỉ muốn di chuyển một số loại tệp trong một thư mục? Sử dụng chức năng

            //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.
            }
5 để giúp lọc các tệp đó trước, sau đó di chuyển chúng từng người một.

5) Di chuyển các tệp đến máy chủ khác

5a-send.php

 curl_file_create($file), // NOTE: PHP 5.5+ ONLY
  "key" => "value" // OPTIONAL EXTRA FIELDS
]);
 
// (B) SEND FILE
$results = curl_exec($curl);
curl_close($curl);
 
// (C) DELETE LOCAL FILE AFTER SUCCESSFUL SEND
if ($results == "OK") {
  unlink($file);
  print_r($results);
}

Thực tế có khá nhiều cách để di chuyển các tệp từ máy chủ này sang máy chủ khác. Đây là một cách của Php Php nếu bạn không muốn thiết lập một máy chủ FTP, ổ đĩa chia sẻ hoặc bất cứ thứ gì thuộc loại đó.

Trên máy chủ nguồn của người dùng, chúng tôi sẽ sử dụng Curl (URL máy khách) để gửi tệp đến máy chủ đích. Sau đó, chúng tôi xóa tập tin sau khi chuyển thành công.

5b-recv.php

Trên máy chủ đích của người dùng, chúng tôi xử lý việc truyền tệp giống như một tệp bình thường tải lên. Nhưng bạn có thể muốn bảo vệ tập lệnh này nhiều hơn một chút - xác minh danh tính của máy khách, hạn chế theo địa chỉ IP hoặc có thể đặt khóa bí mật.

6) Di chuyển các tệp bằng lời nhắc lệnh

6-command-move.php

Cuối cùng, điều này không chính xác sử dụng PHP để di chuyển các tệp, nhưng chúng tôi đang chạy một lệnh shell để di chuyển các tệp từ PHP.

TỔNG KẾT

Hàm số Sự mô tả liên kết tham khảo
            //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.
            }
6
Đổi tên hoặc di chuyển một tập tin.Bấm vào đây
            //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.
            }
7
Kiểm tra nếu tệp đích tồn tại.Bấm vào đây
            //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.
            }
7
Kiểm tra nếu tệp đích tồn tại.Bấm vào đây
            //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.
            }
7
Kiểm tra nếu tệp đích tồn tại.Bấm vào đây
            //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.
            }
7
Kiểm tra nếu tệp đích tồn tại.Bấm vào đây
1

2

3

            //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.
            }
7
Bấm vào đây
            //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.
            }
7
Kiểm tra nếu tệp đích tồn tại.Bấm vào đây

//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. } 7

  • Kiểm tra nếu tệp đích tồn tại.
  •             //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.
                }
    
    8
  • Tạo một thư mục.
  •             //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.
                }
    
    9

Nhận tất cả các tên đường dẫn cho thư mục mẫu/đích đã cho.

Hướng dẫn php move file to another server - php di chuyển tệp sang máy chủ khác
Nhận tệp hoặc tên thư mục theo dõi cho đường dẫn đã cho.

URL máy khách PHP.

4

Làm cách nào để chuyển tệp từ máy chủ này sang máy chủ khác trong PHP?

$ file, "wb");$ nguồn = file_get_contents ($ file);fwrite ($ Destination, $ Source, Strlen ($ Source));fclose ($ Destination);Hình ảnh cần được chuyển sang máy chủ FTP ...
Khởi tạo một phiên trước ..
Các tùy chọn chuyển mong muốn có thể được đặt ..
Việc chuyển nhượng có thể được thực hiện ..
Phiên có thể được đóng ..

Các tệp $ _ trong PHP là gì?

PHP $ _FILES Biến được xác định trước toàn cầu $ _Files là một mảng kết hợp có chứa các mục được tải lên qua phương thức bài HTTP.Tải lên một tệp yêu cầu biểu mẫu phương thức bài HTTP với thuộc tính Enctype được đặt thành nhiều dữ liệu/hình thức.an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.

Làm thế nào để tải lên tệp trong thư mục cụ thể trong PHP?

Tạo tập lệnh tải lên tập lệnh PHP $ target_dir = "Tải lên/" - Chỉ định thư mục nơi tệp sẽ được đặt.$ target_file chỉ định đường dẫn của tệp sẽ được tải lên.$ uploadok = 1 chưa được sử dụng (sẽ được sử dụng sau) $ ImageFileType giữ phần mở rộng tệp của tệp (trong trường hợp thường)$target_dir = "uploads/" - specifies the directory where the file is going to be placed. $target_file specifies the path of the file to be uploaded. $uploadOk=1 is not used yet (will be used later) $imageFileType holds the file extension of the file (in lower case)