Hosting & Domaining Forum

Hosting Discussion => Hosting Security and Technology => Systems Management Requests => Topic started by: bobsmith on Apr 07, 2023, 12:02 AM

Title: Transferring files from one host to another php + ftp
Post by: bobsmith on Apr 07, 2023, 12:02 AM
There is a server that contains over 5000 images, and there is another server where these images need to be transferred. I have created a script that appears to be working, but there are a few issues that I have encountered.

Firstly, every other time the script runs, it encounters a connection error and fails to transmit the images. Secondly, I have not been able to handle large volumes of images yet. Lastly, I would appreciate some advice on how to implement a check for the existence of a file and ensure its file size matches.

Below is the code I have written:

$attachments = $app['db']->fetchAll("SELECT attachment FROM base");
$dest_path = "/a/";
$source_path = DIR . "/b/";

$connection = ftp_connect('...ip...');
$login = ftp_login($connection, login, password);

foreach ($attachments as $attach) {
  $attach = $attach['attachment'];
  $dest = $dest_path . $attach;
  $source = $source_path . $attach;

  if (!$connection || !$login) {
    die('Connection attempt failed! :: ' . $attach);
  }
 
  $upload = ftp_put($connection, $dest, $source, FTP_ASCII);
 
  if (!$upload) {
    echo 'FTP upload failed!';
  }
}

ftp_close($connection);

Title: Re: Transferring files from one hosting to another php + ftp
Post by: messnct on Apr 07, 2023, 01:13 AM
To ensure the stability of FTP, it is crucial to encapsulate each action within a repetitive function. This approach helps handle any potential instability issues that may arise during the transfer process.

However, I am curious about the reasoning behind your current approach. It seems rather unconventional. In my opinion, a more efficient method would involve creating an archive on the first server and transferring it as a single large file to the second server. This approach minimizes the number of individual transfers required and simplifies the overall process.

By adopting this method, you can potentially save time and ensure a smoother transfer experience. Additionally, it allows for easier troubleshooting and reduces the chances of encountering connection errors or transmission failures.
Title: Re: Transferring files from one hosting to another php + ftp
Post by: _XyJIuGaN_ on Apr 07, 2023, 02:17 AM
To resolve your issue, I suggest reaching out to technical support. They can guide you through a simple process that involves using the "tar" command in the terminal to create an archive. Then, you can download this archive onto another hosting platform and unzip it using two additional commands.

Alternatively, you may find that this manual approach is much more efficient than writing complex scripts for the task. It should save significant time as well.

Don't hesitate to contact tech support—they are usually willing to assist with these types of tasks and will likely be able to provide you with clear instructions.
Title: Re: Transferring files from one hosting to another php + ftp
Post by: levimurphynh on Apr 07, 2023, 02:42 AM
To simplify the transfer process, I would recommend compressing the files into an archive on the server, transferring it to the desired location, and then unpacking it. This method can streamline the transfer and ensure all files are successfully transferred.

Another approach that might be easier, especially if the photos are available online, is to directly download and save them on the second machine using functions like file_get_contents or curl. This method eliminates the need for creating an archive and reduces the steps involved in the transfer process.

By considering these alternatives, you can choose the method that best suits your specific scenario and achieve a more efficient and hassle-free transfer of photos.
Title: Re: Transferring files from one host to another php + ftp
Post by: namazob on Dec 22, 2023, 03:58 AM
Regarding the intermittent connection error, it's essential to handle network-related issues gracefully. One way to do this is by implementing a retry mechanism with exponential backoff. This means that when a connection error occurs, the script should wait for an increasing amount of time before attempting to reconnect. This approach helps prevent overwhelming the server with connection attempts and allows the server time to recover if it's experiencing temporary issues.

To improve the efficiency of transferring a large volume of images, consider batching the transfer process. Instead of uploading each image one by one, you can group them into chunks and transfer them in parallel or in sequence, depending on the available resources. This approach can prevent the script from timing out due to prolonged execution and can also reduce the chances of encountering connection errors by spreading the workload more evenly.

For checking the existence of a file and ensuring its file size matches, you can use FTP commands such as SIZE to verify the file's size and MDTM to check the file's modification timestamp. By implementing these checks, you can ensure that the transferred files are complete and accurate.

In addition to these technical improvements, consider implementing robust logging and monitoring within the script. Logging the transfer status, any encountered errors, and additional diagnostic information can help you understand the behavior of the script and troubleshoot any issues more effectively. Monitoring the transfer process in real-time can also provide insights into performance bottlenecks or unexpected behavior.
Addressing these issues will make the image transfer script more reliable, efficient, and maintainable, ensuring smooth and error-free transfer of images between servers.
Title: Re: Transferring files from one host to another php + ftp
Post by: MesyHeittee on Aug 01, 2025, 12:08 PM
You should move the ftp_connect and ftp_login calls outside the loop to avoid reconnecting every iteration, which kills performance and stability. Use FTP_BINARY for images, not FTP_ASCII. For large batches, chunk uploads and implement exponential backoff on failure.
To verify files, use ftp_size() to compare remote file size with local before skipping or overwriting.