How does php import/export produce a progress bar ?? -Php Tutorial

Source: Internet
Author: User
How does php import/export produce a progress bar ?? I want to use the import/export function. to make the interface friendly, I want to display the progress of the qualifying progress bar when prompted, but I don't know how to do it!
My import is to import a csv file and read it directly. Then, in the insert into data table, the export is also a direct query to generate a csv file,
I can make a progress bar, but I don't know how to get the import and export progress, and then update my progress bar through ajax and js.


Reply to discussion (solution)

Client support, through
Sliverlight, flash, html5

Or on the server side, you can check the apc extension. it seems that you have forgotten the name.

This is easily implemented using ajax. you can check the relevant information.

If there are 100 operations, you can move the first operation forward 1 and the second operation forward 3...

I do not recommend this progress bar effect because it may affect the speed.
Just get a "importing"

Use cache output


// Omit other html + js progress bar code $ I = 0; while ($ I <100) {// execute content sleep (1 ); // calculate the task progress based on the total amount and the current task $ I = $ I ++; echo 'script progress bar changed js '. $ I. 'script '; ob_flash () flash ();}

 Document. getElementById (\ 'p \ '). innerHTML = "'. $ I. '%"; script'; ob_flush (); flush ();}

I wrote it when I used to collect data. it is useful in ie. chrome does not run during the next try.
Http://devp.cosrc.com/scroll2.php

Very good, very strong >>>

PHP code
// Omit other html + js progress bar code
$ I = 0;
While ($ I <100 ){
// Execution content
Sleep (1 );
// Calculate the task progress based on the total amount and the current task $ I
$ I = $ I ++;
Echo 'script progress bar changes js to '. $ I.' script ';
Ob_flash ()
Flash ();
}
I will make a progress bar. The key is that I will not do this step "// calculate the progress of the task based on the total amount and the current task $ I"
How can I perform this step?

Reference the reply from hnxxwyq on the 6th floor:
PHP code
// Omit other html + js progress bar code
$ I = 0;
While ($ I <100 ){
// Execution content
Sleep (1 );
// Calculate the task progress based on the total amount and the current task $ I
$ I = $ I ++;
Echo 'script progress bar changes js to '. $ I.' script ';
Ob_flash ()
Flash ();
}

......

Normally, this progress bar does this.
Obtain the overall task first, then quantify it in one dimension, and then take the current progress at any time, divide it by the overall, and then complete it.

For example, if x rows in a file are to be read, although the content in each row is different in size, the overall progress is 1/x after each row is calculated.

If you want to finish processing a row, you need to respond to the constant query and output, which will affect the performance, and your import method must be able to quantify each step.

For example
$ File_size = number of data rows in the file;
$ Current = 0; // The first line, start

While (not completed)
{
Import a row;
$ Current ++;
Output $ current/$ file_size * 100;
// Continue
}

Obviously, your processing is gradual... if you use a ready-made function to directly import and export files, you will not get the intermediate execution process.






Reference the reply from hnxxwyq on the 6th floor:
PHP code
// Omit other html + js progress bar code
$ I = 0;
While ($ I <100 ){
// Execution content
Sleep (1 );
// Calculate the task progress based on the total amount and the current task $ I
$ I = $ I ++;
Echo 'script progress bar changes js to '. $ I.' script ';
Ob_flash ()
Flash ();
}

......

During the import, you can calculate the total number of csv rows n, and then read the m rows at a fixed time (which can be corrected based on the efficiency you think). each time you process m rows, record the number of tasks executed at a time. The final progress ratio is floor (I * m/n) * 100;

Pseudocode

  

Reply to reference wxhbbdd on the 10th floor:

Reference the reply from hnxxwyq on the 6th floor:
PHP code
// Omit other html + js progress bar code
$ I = 0;
While ($ I <100 ){
// Execution content
Sleep (1 );
// Calculate the task progress based on the total amount and the current task $ I
$ I = $ I ++;
Echo 'script progress bar changes js to '. $ I.' script ';
Ob ......



$ N = 'Total task Qty ';
$ M = 'batch task executions ';
$ I = 0;

While ($ I * $ m <$ n ){
// Process m executions

// Count the number of executed tasks
$ Total = ++ $ I * $ m;

// Calculate the progress ratio
$ P = floor (min ($ total, $ n)/$ n * 100); // less than 100
}

APC implementation method:

Install APC by referring to the official documentation. you can use the PECL module to install APC quickly and easily.
Configure php. ini and set the parameter apc. rfc1867 = 1 to enable APC to support the upload progress bar function, which is described in the APC source code instructions.
Sample code:
If ($ _ SERVER ['request _ method'] = 'post') {// upload REQUEST
$ Status = apc_fetch ('upload _ '. $ _ POST ['apc _ UPLOAD_PROGRESS']);
$ Status ['Done'] = 1;
Echo json_encode ($ status); // ajax call output to the user's page. please find the relevant documentation
Exit;
} Elseif (isset ($ _ GET ['SS SS _ key']) {// read the upload progress
$ Status = apc_fetch ('upload _ '. $ _ GET ['SS SS _ key']);
Echo json_encode ($ status );
Exit;
} Else {
// Other code, such as uploading a form
}

Implementation of the uploadprogress module:
Use the PECL module installation method to install this module
Set uploadprogress. file. filename_template = "/tmp/upd_%s.txt" in php. ini"
Sample code:
If ($ _ SERVER ['request _ method'] = 'post '){
If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
$ Upload_dir = 'Your _ path /';
$ Ext = strrchr ($ _ FILES ['video'] ['name'], '.');
$ Sessid = $ _ POST ['upload _ IDENTIFIER '];
$ Tmpfile = $ upload_dir. $ sessid;
$ Sessfile = $ upload_dir. $ sessid. $ ext;
If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ tmpfile )){
// Upload successful
} Else {
// Upload failed
} Else {
// Upload error

} Elseif (! Empty ($ _ GET ['sessid ']) {
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
Header ("Cache-Control: no-store, no-cache, must-revalidate ");
Header ("Cache-Control: post-check = 0, pre-check = 0", false );
Header ("Pragma: no-cache ");
Header ("Content-Type: text/html; charset = UTF-8 ");

$ Unique_id = $ _ GET ['sessid'];
$ Uploadvalues = uploadprogress_get_info ($ unique_id );

If (is_array ($ uploadvalues )){
Echo json_encode ($ uploadvalues );
} Else {
// Read progress failed. additional processing logic
}

} Else {
// Display the upload form
}

Add to favorites ~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.