Guest User

Untitled

a guest
Sep 18th, 2018
1,940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. PHP - Merging files with FTP
  2. <?php
  3.  
  4. // FTP credentials
  5. $server = HIDDEN;
  6. $username = HIDDEN;
  7. $password = HIDDEN;
  8.  
  9. // Connect to FTP
  10. $connection = ftp_connect($server) or die("Failed to connect to <b>$server</b>.");
  11.  
  12. // Login to FTP
  13. if (!@ftp_login($connection, $username, $password))
  14. {
  15. echo "Failed to login to <b>$server</b> as <b>$username</b>.";
  16. }
  17.  
  18. // Destination file (where the copied file should go)
  19. $destination = 'final.txt';
  20.  
  21. // The file on my server that we're copying (in chunks) to $destination.
  22. $read = 'readme.txt';
  23.  
  24. // Current chunk of $read.
  25. $temp = 'temp.tmp';
  26.  
  27. // If the file we're trying to copy exists...
  28. if (file_exists($read))
  29. {
  30. // Set a chunk size (this is tiny, but I'm testing
  31. // with tiny files just to make sure it works)
  32. $chunk_size = 4;
  33.  
  34. // For reading through the file we want to copy to the FTP server.
  35. $read_handle = fopen($read, 'r');
  36.  
  37. // For writing the chunk to its own file.
  38. $temp_handle = fopen($temp, 'w+');
  39.  
  40. // Loop through $read until we reach the end of the file.
  41. while (!feof($read_handle))
  42. {
  43. // Read a chunk of the file we're copying.
  44. $chunk = fread($read_handle, $chunk_size);
  45.  
  46. // Write that chunk to its own file.
  47. fwrite($temp_handle, $chunk);
  48.  
  49. ////////////////////////////////////////////
  50. //// ////
  51. //// NOW WHAT?? HOW DO I ////
  52. //// WRITE / APPEND THAT ////
  53. //// CHUNK TO $destination? ////
  54. //// ////
  55. ////////////////////////////////////////////
  56. }
  57. }
  58.  
  59. fclose($read_handle);
  60. fclose($temp_handle);
  61. ftp_close($ftp_connect);
  62. ?>
  63.  
  64. <?php
  65. // The URI of the remote file to be written to
  66. $write = 'ftp://username1:password1@domain1.com/path/to/writeme.txt';
  67.  
  68. // The URI of the remote file to be read
  69. $read = 'ftp://username2:password2@domain2.com/path/to/readme.txt';
  70.  
  71. if (file_exists($read)) // this will work over ftp too
  72. {
  73. $chunk_size = 4;
  74. $read_handle = fopen($read, 'r');
  75. $write_handle = fopen($write, 'a');
  76.  
  77. while (!feof($read_handle))
  78. {
  79. $chunk = fread($read_handle, $chunk_size);
  80. fwrite($write_handle, $chunk);
  81. }
  82. }
  83.  
  84. fclose($read_handle);
  85. fclose($write_handle);
  86. ?>
Add Comment
Please, Sign In to add comment