File Upload With Progress Bar Using PHP

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

guiguiboy/PHP-CLI-Progress-Bar

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A PHP5 CLI Progress bar Version 0.0.4

There is one namespace ProgressBar that contains 2 classes Manager and Registry.

Manager is responsible to manage the progress bar. Each instance of this class is associated with a Registry object. Each time the Manager needs to keep a metric, it is stored in this object.

When the display is requested, the script uses the string format and iterates over all replacement rules. Replacements are handled by closures.

The progress bar has the following default output : %current%/%max% [%bar%] %percent%% %eta%

It is configurable. You can also change it while processing your batch script.

Buit-in variable replacement are :

  • %current% : the current element
  • %max% : the number of elements
  • %bar% : the progress bar
  • %percent% : the advancement in percent
  • %eta% : estimation of the remaining

Manager constructor arguments :

  • current : the initial step
  • max : the amount of steps in your process
  • width : the max width of the line (default : 80)
  • doneBarElementCharacter : a character to identify done advancement in the progress bar (default : =)
  • remainingBarElementCharacter : a character to identify remaining advancement in the progress bar (default : -)
  • currentPositionCharacter : a character to identify the current position in the progress bar (default : >)
Читайте также:  Css gradient shadow boxing

Add include statements at the beginning of your script (if you don’t have autoloaders)

 require_once 'ProgressBar/Manager.php'; require_once 'ProgressBar/Registry.php'; $progressBar = new \ProgressBar\Manager(0, 10); for ($i = 0; $i 10; $i++) < $progressBar->update($i); sleep(1); >

When you want to iterate over a collection, you don’t event need to track the counter:

foreach ($array as $element) < // process element $progressBar->advance(); >
$progressBar->setFormat('%current% |%bar%| %max%'); $progressBar->update(1);

The max length is specified in the constructor :

$pb = new \ProgressBar\Manager(0, 20, 120); $progressBar->update(1);

Changing the progress bar style

Use the parameters specified in the constructor :

$pb = new \ProgressBar\Manager(0, 20, 120, '-', ' ', ')'); $pb->update(5);

Adding custom replacement rules

You may add your custom variables for replacement. You have to use the method addReplacementRule and specify a priority, a tag and a closure. Keep in mind that the length of the progress bar is evaluated at the end so that the output width will scale up to the width you specified. So keeping the %bar% with a high priority is a good practice.

Here is an example of what you should do if you want to add a new replacement rule.

 use ProgresBar; $pb = new Manager(0, 213); $pb->setFormat('Progress : %current%/%max% [%bar%] %foo%'); $pb->addReplacementRule('%foo%', 70, function ($buffer, $registry) return 'OK!';>); $pb->update(1);
  • Changed RuntimeExceptions to InvalidArgumentExceptions
  • Added advance() method for incrementing the progress bar
  • Forbid to set the progress value greater than expected
  • Changed directory structure to add namespace
  • Changed priority behavior
  • Added new public method addReplacementRule so that you don’t need to extend the manager to add custom replacement rules

Источник

Progress bar with php

A progress bar can be used to indicate the status of any PHP function running in the background. I will show you the easiest way to create a real-time progress bar to monitor the status of your PHP script running on the server just by using simple pure PHP code and some javascript.

If you are a PHP developer then probably you notice that you will not get the output in your view page unless the PHP script is completely executed. You cannot even use Ajax as ajax success function only triggered when PHP script completed its execution. So the trick is to forcefully flush the output to view the page using flush and ob_flush function in PHP and javascript to update your progress bar in real time instead of using ajax calls.

Here is the code for view page

 

 

 

 

 

 


I use bootstrap for a little styling and here is the javascript code you must include at the bottom of your view page.

  

When button1 is clicked jquery click function will be triggered which will result in loading the progressbar.php file in an iframe. The button2 is used to stop the progress bar.

Here are the codes in progressbar.php file

 $total = 100; for($i=$_SESSION['i'];$i <$total;$i++) < $_SESSION['i'] = $i; $percent = intval($i/$total * 100)."%"; sleep(1); // Here call your time taking function like sending bulk sms etc. echo ''; ob_flush(); flush(); > echo ''; session_destroy(); 

I use the sleep function to demonstrate a time taking task. You can call your own time taking function there like sending bulk SMS or mail to users etc. flush() is a low-level flush that instructing PHP to flush its internal, low-level data buffers and ob_flush() is a high-level flush. It flushes high-level buffers and puts all content in the low-level.

Here is the output of above program

Real time progress bar output

You must use both flush and ob_flush function to make progress bar real time. If you don’t use one of them then your progress bar update only in certain intervals of time and not in real time. Also, don’t forget to set maximum execution time to unlimited otherwise progress bar stop after 30 seconds (Default maximum PHP script execution time). Comment below if you have any doubts or occur any problems.

Источник

Progress Bar In PHP With Source Code

Progress Bar in PHP with Source Code 2021

A Progress Bar In PHP is a graphical control element that shows how far along a long computer process, like a download, file transfer, or installation, Sometimes the picture is accompanied by text that shows the progress as a percentage.

About The Project

The Progress Bar was developed using PHP, HTML, CSS, jQuery, AJAX and JavaScript. This Dynamic Progress Bar In PHP is use when we are uploading any file type in our system or websites.

In File Upload With Progress Bar In PHP we are going to make File Upload Progress Bar In PHP Example. In the example below, we have file input field and progress bar.

During the Upload With Progress Bar PHP, we are going to show the progress bar if it is done already. And, also, we have to use the jQuery and AJAX function to do this functionally.

In this tutorial you will learn on How To Make Progress Bar in an easy way and a short period of time. and also this tutorial is easy to understand the codes and process that are good for the beginners or the students.

So, lets start the tutorial..

To start executing this Simple Project makes sure that you have sublime or any platform of PHP and MySQL installed in your computer.

Project Information’s

Steps On How To Create The Progress Bar In PHP With Source Code

    Create folder.

progress bar project folder

First, create a folder from your “xampp” folder under “htdocs” folder and name it.

Open folder.

progress bar open folder

Second, open “sublime text” and open the project folder you’ve created.

Create a php file.

progress bar create php file

Third, create the php file under your project folder and name it into “index.php“.

How To Create A Progress Bar In PHP?

HTML Form

– The code given below which is the html form of this simple project, you can also copy the codes given below and paste it into your file.

 

jQuery Script

– The code given below which is the jQuery Script of this simple project, you can also copy the codes given below and paste it into your file.

   

CSS Style

– The code given below which is the CSS design of this simple project, you can also copy the codes given below and paste it into your file.

 #upload_container < border-top:#F0F0F0 2px solid; background:#FAF8F8; padding:10px; width:600px; >#upload_container label < margin:2px; font-size:1em; font-weight:bold; >.demoInputBox < padding:5px; border:#F0F0F0 1px solid; border-radius:4px; background-color:#FFF; >#progress-bar < background-color: skyblue; height:20px;color: #FFFFFF; width:0%; -webkit-transition: width .3s; -moz-transition: width .3s; transition: width .3s; >.btnSubmit < background-color:azure; padding:10px 40px; color:blue; border:skyblue 1px solid; border-radius:4px; >#progress-div < border:blue 2px solid; padding: 5px 0px; margin:30px 0px; border-radius:4px; text-align:center; >#targetLayer < width:100%; text-align:center; >#targetLayer img

Progress Bar Complete Source Code

     #upload_container < border-top:#F0F0F0 2px solid; background:#FAF8F8; padding:10px; width:600px; >#upload_container label < margin:2px; font-size:1em; font-weight:bold; >.demoInputBox < padding:5px; border:#F0F0F0 1px solid; border-radius:4px; background-color:#FFF; >#progress-bar < background-color: skyblue; height:20px;color: #FFFFFF; width:0%; -webkit-transition: width .3s; -moz-transition: width .3s; transition: width .3s; >.btnSubmit < background-color:azure; padding:10px 40px; color:blue; border:skyblue 1px solid; border-radius:4px; >#progress-div < border:blue 2px solid; padding: 5px 0px; margin:30px 0px; border-radius:4px; text-align:center; >#targetLayer < width:100%; text-align:center; >#targetLayer img      

Download Source Code below

Anyway, if you want to level up your programming knowledge, especially PHP, try this new article I’ve made for you Best PHP Projects With Source Code Free Download 2021.

Summary

This Simple Project With Source Code was developed using PHP, HTML, CSS, jQuery, AJAX and JavaScript. In this tutorial you will learn on How To Make Progress Bar in an easy way and a short period of time. and also this tutorial is easy to understand the codes and process that are good for the beginners or the students.

Inquiries

If you have any questions or suggestions about Progress Bar In PHP With Source Code , please feel free to leave a comment below.

Источник

Оцените статью