Update progress bar from database
-
I’m so very close to being able to release a free plugin.
Upon uploading media, my plugin automatically transfers audio and video files to an external server (Archive.org). The part that I’m hung up on is the progress bar, showing the progress of transferring the file to Archive.org. But instead of a smoothly growing progress bar, it stays at 0% and then jumps to 100% once it’s done.
Relevant code from the plugin:
// Set all the options if they don't exist yet add_option('aou_accesskey',""); add_option('aou_secretkey',""); add_option('aou_progress',""); update_option('aou_progress','0'); function progressCallback( $download_size, $downloaded_size, $upload_size, $uploaded_size ){ static $previousProgress = 0; if ( $upload_size == 0 ) $progress = 0; else $progress = round( $uploaded_size * 100 / $upload_size ); if ( $progress > $previousProgress){ $previousProgress = $progress; update_option('aou_progress',$progress); } } function aou_upload_file($file) { $ias3accesskey = get_option('aou_accesskey',""); $ias3secretkey = get_option('aou_secretkey',""); $filename = str_replace(' ', '-', $file['name']); $file_no_ext = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename); $file_read = fopen($file['tmp_name'], 'r'); $upload_file_url = 'https://archive.org/download/' . $filename . '/' . $filename; $headers = array( 'Authorization: LOW ' . $ias3accesskey . ':' . $ias3secretkey, 'x-amz-auto-make-bucket:1', 'x-archive-meta01-collection:opensource_' . $mediatype, 'x-archive-meta-title:' . $file['name'] ); $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, 'https://s3.us.archive.org/' . $filename . '/' . $filename); curl_setopt($ch, CURLOPT_PUT, true); curl_setopt($ch, CURLOPT_INFILE, $file_read); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file['tmp_name'])); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_NOPROGRESS, false ); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback'); curl_setopt($ch, CURLOPT_BUFFERSIZE, 25); curl_exec($ch); curl_close($ch); fclose($file_read); return $file; } add_filter('wp_handle_upload_prefilter','aou_upload_file'); function aou_css_head() { global $pagenow; if ($pagenow == 'media-new.php'){ ?> <style type="text/css"> #aou_progress { display:block; height:15px; background-color:#ddd; text-align:right; overflow:hidden; font-size:10px; color:#aaa; position:relative; padding-right:3px; } #aou_progress_bar { position:absolute; top:0; left:0; width:0%; height:15px; background-color:#cdf; padding-right:3px; overflow:hidden; } </style> <script> window.setInterval(function(){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { var progressElement = document.getElementById('aou_progress_bar'); if (xhttp.readyState == 4 && xhttp.status == 200 && progressElement.innerHTML != '100%') { progressElement.style.width = xhttp.responseText + '%'; progressElement.innerHTML = xhttp.responseText + '%'; } }; xhttp.open("GET", "https://<?php echo $_SERVER['SERVER_NAME']; ?>/wp-content/plugins/archiveorg-uploads/progress.php", true); xhttp.send(); }, 125); </script> <?php } } add_action('admin_head', 'aou_css_head'); function aou_progress_bar() { global $pagenow; if ($pagenow == 'media-new.php'){ echo '<div id="aou_progress">Archive.org Uploads Progress<div id="aou_progress_bar">0%</div></div>'; } }
add_action(‘all_admin_notices’,’aou_progress_bar’);`
<strong>progress.php</strong>
<?php require_once('../../../wp-load.php'); global $wpdb; echo $wpdb->get_var( "SELECT option_value FROM wp_options WHERE option_name='aou_progress'" ); ?>
So, so basically, the callback of the upload updates the aou_progress option in the database, and this option is written via progress.php, which should be checked for changes every 1/8 second, and the progress bar should grow.
But obviously I did something wrong here. I’m wondering if it can’t check progress.php for a change while it’s transferring the file via curl? If that’s true, is there a way to get around it?
- The topic ‘Update progress bar from database’ is closed to new replies.