If you're performing the FTP syncing process within the same request, then yes your page will take a very long while to load and might interfere with other requests as well.
The suggested way of doing this is usually to make a job queue, in which you can use a storage system (database, etc) or a message queue to queue the jobs, and write another script to perform the syncing.
If you want an easier way to solve this, you can just
- Use
ignore_user_abort()
and flush
to cut away the user connection and continue the syncing in the background
- Embed a frame within the page which is performing the action and show a progressbar. Then at the end of the sync do a
window.parent.location = 'done-syncing'
For 1., here's an example:
<?php
ignore_user_abort(true);
ob_start();
echo 'Syncing...';
ob_end_flush();
flush();
// Perform the FTP syncing here
For 2., it will span across multiple pages. Suppose clicking on "Push files to FTP" brings you to a page ftp_sync_start.php
, and then the file which does the real syncing is ftp_do_sync.php
, then here are some examples:
ftp_sync_start.php
<!-- You can put some sort of progress bar here -->
<iframe src="ftp_do_sync.php?<?php // Pass some parameters here ?>"></iframe>
ftp_do_sync.php
<?php
// Just do the syncing here, then at the end
echo "<script>window.parent.location = 'ftp_sync_done.php';</script>";
These solutions are not too scalable though.