cvs: pear /HTML_Progress2/Progress2 Upload.php

From: Date: Mon, 12 Feb 2007 16:14:54 +0000
Subject: cvs: pear /HTML_Progress2/Progress2 Upload.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-44629@lists.php.net to get a copy of this message
farell Mon Feb 12 16:14:54 2007 UTC Added files: /pear/HTML_Progress2/Progress2 Upload.php Log: HTML_Progress2_Upload class: initial commit http://cvs.php.net/viewvc.cgi/pear/HTML_Progress2/Progress2/Upload.php?view=markup&rev=1.1 Index: pear/HTML_Progress2/Progress2/Upload.php +++ pear/HTML_Progress2/Progress2/Upload.php <?php /** * Backend for AJAX uploading bar. * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.01 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_01.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category HTML * @package HTML_Progress2 * @author Laurent Laville <pear@laurent-laville.org> * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @version CVS: $Id: Upload.php,v 1.1 2007/02/12 16:14:54 farell Exp $ * @link http://pear.php.net/package/HTML_Progress2 * @since File available since Release 2.3.0RC1 */ /** * Backend for AJAX uploading bar. * * @category HTML * @package HTML_Progress2 * @author Laurent Laville <pear@laurent-laville.org> * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_Progress2 * @since Class available since Release 2.3.0RC1 */ class HTML_Progress2_Upload { /** * Backend identifier are either : * 'none' - if nothing else below match * 'apc5' - APC php extension with PHP5.2.0 or greater * 'upm4' - upload progress meter php extension with PHP4 * 'upm5' - upload progress meter php extension with PHP5.2.0 or greater * * @var string * @since 2.3.0RC1 * @access public */ var $backend; /** * Formatted strings for Progress bar labels that will display upload info. * * Each conversion specification consists of a percent sign (%), followed * by one or more of these elements. * T - for display total file size * C - for display current file size uploaded * P - for display percentage of file size uploaded * * @var string * @since 2.3.0RC1 * @access public * @see HTML_Progress2_Upload::sprintf() */ var $format; /** * Constructor (ZE2) Summary * * @param string $format (optional) conversion specifications * for all progress bar labels * @since 2.3.0RC1 * @access protected * @link http://pdoru.from.ro/upload-progress-meter/ Patch for PHP 4 */ function __construct($format = null) { $php5 = (version_compare(phpversion(), '5.2.0', 'ge') < 0) ? false : true; if (extension_loaded('upload_progress_meter')) { $this->backend = 'upm4'; } elseif (extension_loaded('uploadprogress') && $php5) { $this->backend = 'upm5'; } elseif (extension_loaded('apc') && $php5) { $this->backend = 'apc5'; } else { $this->backend = 'none'; } if (!is_array($format)) { $format = array('uplStatus' => '%C / %T (%P%)'); } $this->format = $format; } /** * Return hash of data with current file upload information, depending of * backend used. * * @param string $Id upload identifier * * @return bool|array false if info unavailable, hash of data otherwise * @since 2.3.0RC1 * @access public */ function getInfo($Id) { $info = array('time_start' => 0, 'time_last' => 0, 'speed_average' => 0, 'speed_last' => 0, 'est_sec' => -1, 'bytes_uploaded' => 0, 'bytes_total' => 0, 'files_uploaded' => 0, 'cancel_upload' => 0 ); switch ($this->backend) { case 'upm4': $tmp = upload_progress_meter_get_info($Id); if (is_array($tmp)) { $info = array_merge($info, $tmp); } else { $info = false; } break; case 'upm5': $tmp = uploadprogress_get_info($Id); if (is_array($tmp)) { $info = array_merge($info, $tmp); } else { $info = false; } break; case 'apc5': $tmp = apc_fetch('upload_' . $Id); if (is_array($tmp)) { $info['bytes_uploaded'] = $tmp['current']; $info['bytes_total'] = $tmp['total']; $info['files_uploaded'] = $tmp['done']; $info['cancel_upload'] = $tmp['cancel_upload']; } else { $info = false; } break; default: } return $info; } /** * Return a formatted string with current upload information * * Each conversion specification consists of a percent sign (%), followed * by one or more of these elements. * T - for display total file size * C - for display current file size uploaded * P - for display percentage of file size uploaded * * @param string $format conversion specification * @param array $info current upload information * @return bool|string false on error, formatted string with current info * @since 2.3.0RC1 * @access public */ function sprintf($format, $info) { if (!is_array($info)) { return false; } $pos = strpos($format, '%T'); if ($pos !== false) { $format = str_replace('%T', $this->formatBytes($info['bytes_total']), $format); } $pos = strpos($format, '%C'); if ($pos !== false) { $format = str_replace('%C', $this->formatBytes($info['bytes_uploaded']), $format); } $pos = strpos($format, '%P'); if ($pos !== false) { if ($info['bytes_total'] < 1) { $percent = 100; } else { $percent = round($info['bytes_uploaded'] / $info['bytes_total'] * 100); } $format = str_replace('%P', $percent, $format); } return $format; } /** * Get the status of an upload passed in * * @param string $uplId upload identifier * @return array * @since 2.3.0RC1 * @access public */ function getStatus($uplId = null) { if (!isset($uplId)) { $uplId = $_GET['upload_identifier']; } $tmp = $this->getInfo($uplId); if (!is_array($tmp)) { $ret = array('percentage' => 100, 'labels' => array()); return $ret; } $percent = $this->sprintf('%P', $tmp); $labels = array(); foreach ($this->format as $lbl => $fmt) { $labels[$lbl] = $this->sprintf($fmt, $tmp); } $ret = array('percentage' => $percent, 'labels' => $labels, 'bytes_uploaded' => $tmp['bytes_uploaded'], 'bytes_total' => $tmp['bytes_total'] ); return $ret; } /** * Function to convert bytes to something larger * * @param int $x file size in bytes * @return string * @since 2.3.0RC1 * @access public */ function formatBytes($x) { if ($x < 1024) { return "$x bytes"; } elseif ($x < (1024 * 1024)) { return sprintf("%d KB", $x/1024); } else { return sprintf("%d MB", $x/(1024 * 1024)); } } } ?>

« previous php.pear.cvs (#44629) next »