Pull the scrollbar down during loading

Hello,

I am doing something similar to the code below with a browser popup.
What I want to do is to pull the scroll bar down as the results are printed on the screen.

I tried many things but no results.
Is there a way to keep the scroll bar always down while running PHP?

ob_start();

    function fls()
    {
        ob_end_flush();
        if (ob_get_level() > 0) {ob_flush();}
        flush();
        ob_start();
    }

for ($i = 1; $i <= 50; $i++) {
    echo $i."<br>"; fls();
    sleep(1);
}

In this online php trial terminal, the scroll bar goes down, I want to do the same.
Online PHP Compiler (Interpreter)

To automatically scroll down in a browser while printing results with PHP, you can use JavaScript. PHP by itself cannot control the browser’s UI. However, with the help of JavaScript, you can manipulate the browser’s DOM and behaviors like scrolling.

Here’s a way to do it:

  1. Include a <script> block to run a JavaScript function that scrolls the page down every time new content is added.
  2. Modify your PHP to trigger this JavaScript function after each flush.

Do it like this:

ob_start();

function fls() {
    ob_end_flush();
    if (ob_get_level() > 0) { ob_flush(); }
    flush();
    ob_start();
    echo '<script>scrollToBottom()</script>'; // Trigger the JavaScript function
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Scroll Down</title>
</head>
<body>
<script>
    function scrollToBottom() {
        window.scrollTo(0,document.body.scrollHeight); // Scroll to the bottom of the document
    }
</script>

<?php

for ($i = 1; $i <= 50; $i++) {
    echo $i."<br>";
    fls();
    sleep(1);
}

?>

</body>
</html>

In the example above, after each number is printed and the buffer is flushed, we execute the JavaScript function scrollToBottom() that scrolls the page to its bottom. The result should be a continuous scrolling effect as new numbers are printed.

Keep in mind that this approach relies on the combination of PHP’s output buffering and JavaScript’s control over the browser UI. Your web server also needs to support flushing content in real time for this to work correctly. Some servers might buffer the output at the server level, causing this approach not to work.

Good luck

1 Like

I tried it locally and it worked.
I’ll try it on the remote server later.
Thank you

This code is ftp code with php and the transferred files are listed.
I had to move a server and I had a website with a size of 20.5 GB.
I transferred the files in a few hours using FTP code with PHP.
While it printed every line while trying it on the local server, it printed at intervals of 5-10-20 lines on the remote server. For large files such as videos, it did not print at all. What is the reason for this or is there a solution?

Additionally, file transfer continued even though it gave errors such as timeout. What could be the reason for this?

I transferred large files in a separate directory and the others in a separate directory.

I used this code and made some edits

@Adem, Dedicated FTP Clients - should be preferred to transfer files, it might be more efficient and informative to use a dedicated FTP client which will give detailed logs and progress recommended FTP clients are ;

  1. FileZilla:
  • Platform: Windows, macOS, Linux
  • Features: Supports FTP, SFTP, and FTPS, easy-to-use interface, drag-and-drop file transfers, and more.
  1. WinSCP:
  • Platform: Windows
  • Features: Primarily an SFTP and SCP client, but also supports FTP and WebDAV. Integrated text editor, synchronization functionality, scripting, and more.
  1. Cyberduck:
  • Platform: Windows, macOS
  • Features: Supports FTP, SFTP, WebDAV, Amazon S3, and OpenStack Swift. Easy to use, drag-and-drop interface, integration with external editors.

I mostly use FileZilla. Use any selected client to re-upload the website.

I use FileZilla on my computer and I am very happy with it
Making 20gb zip takes a lot of time
Downloading 20 GB still takes a lot of time
Upload 20 GB takes even more time
I transferred it to the new server within 1 hour via PHP FTP

I am working on a PHP FTP website to automatically backup site files and database backups to a remote server.

I don’t know how true it is, someone gave an idea.
In the meantime, it was necessary to change the server and I used it.
I downloaded my other normal-sized websites as a zip file with FileZilla and transferred them to the new server.

Sponsor our Newsletter | Privacy Policy | Terms of Service