📄 file.class.php
字号:
<?php/* vim: set expandtab sw=4 ts=4 sts=4: *//** * file upload functions * * @version $Id: File.class.php 11536 2008-09-02 17:35:48Z nijel $ *//** * * @todo replace error messages with localized string * @todo when uploading a file into a blob field, should we also consider using * chunks like in import? UPDATE `table` SET `field` = `field` + [chunk] */class PMA_File{ /** * @var string the temporary file name * @access protected */ var $_name = null; /** * @var string the content * @access protected */ var $_content = null; /** * @var string the error message * @access protected */ var $_error_message = ''; /** * @var bool whether the file is temporary or not * @access protected */ var $_is_temp = false; /** * @var string type of compression * @access protected */ var $_compression = null; /** * @var integer */ var $_offset = 0; /** * @var integer size of chunk to read with every step */ var $_chunk_size = 32768; /** * @var resource file handle */ var $_handle = null; /** * @var boolean whether to decompress content before returning */ var $_decompress = false; /** * @var string charset of file */ var $_charset = null; /** * @staticvar string most recent BLOB repository reference */ static $_recent_bs_reference = NULL; /** * constructor * * @access public * @uses PMA_File::setName() * @param string $name file name */ function __construct($name = false) { if ($name) { $this->setName($name); } } /** * destructor * * @see PMA_File::cleanUp() * @access public * @uses PMA_File::cleanUp() */ function __destruct() { $this->cleanUp(); } /** * deletes file if it is temporary, usally from a moved upload file * * @access public * @uses PMA_File::delet() * @uses PMA_File::isTemp() * @return boolean success */ function cleanUp() { if ($this->isTemp()) { return $this->delete(); } return true; } /** * deletes the file * * @access public * @uses PMA_File::getName() * @uses unlink() * @return boolean success */ function delete() { return unlink($this->getName()); } /** * checks or sets the temp flag for this file * file objects with temp flags are deleted with object destruction * * @access public * @uses PMA_File::$_is_temp to set and read it * @param boolean sets the temp flag * @return boolean PMA_File::$_is_temp */ function isTemp($is_temp = null) { if (null !== $is_temp) { $this->_is_temp = (bool) $is_temp; } return $this->_is_temp; } /** * accessor * * @access public * @uses PMA_File::$_name * @param string $name file name * @access public */ function setName($name) { $this->_name = trim($name); } /** * @access public * @uses PMA_File::getName() * @uses PMA_File::isUploaded() * @uses PMA_File::checkUploadedFile() * @uses PMA_File::isReadable() * @uses PMA_File::$_content * @uses function_exists() * @uses file_get_contents() * @uses filesize() * @uses fread() * @uses fopen() * @uses bin2hex() * @return string binary file content */ function getContent($as_binary = true, $offset = 0, $length = null) { if (null === $this->_content) { if ($this->isUploaded() && ! $this->checkUploadedFile()) { return false; } if (! $this->isReadable()) { return false; } if (function_exists('file_get_contents')) { $this->_content = file_get_contents($this->getName()); } elseif ($size = filesize($this->getName())) { $this->_content = fread(fopen($this->getName(), 'rb'), $size); } } if (! empty($this->_content) && $as_binary) { return '0x' . bin2hex($this->_content); } if (null !== $length) { return substr($this->_content, $offset, $length); } elseif ($offset > 0) { return substr($this->_content, $offset); } return $this->_content; } /** * @access public * @uses PMA_File::getName() * @uses is_uploaded_file() */ function isUploaded() { return is_uploaded_file($this->getName()); } /** * accessor * * @access public * @uses PMA_File::$name as return value * @return string PMA_File::$_name */ function getName() { return $this->_name; } /** * @todo replace error message with localized string * @access public * @uses PMA_File::isUploaded() * @uses PMA_File::setName() * @uses PMA_File::$_error_message * @param string name of file uploaded * @return boolean success */ function setUploadedFile($name) { $this->setName($name); if (! $this->isUploaded()) { $this->setName(null); $this->_error_message = 'not an uploaded file'; return false; } return true; } /** * @access public * @uses PMA_File::fetchUploadedFromTblChangeRequestMultiple() * @uses PMA_File::setUploadedFile() * @uses PMA_File::setRecentBLOBReference() * @uses curl_setopt_array() * @uses PMA_File::$_error_message * @uses $GLOBALS['strUploadErrorIniSize'] * @uses $GLOBALS['strUploadErrorFormSize'] * @uses $GLOBALS['strUploadErrorPartial'] * @uses $GLOBALS['strUploadErrorNoTempDir'] * @uses $GLOBALS['strUploadErrorCantWrite'] * @uses $GLOBALS['strUploadErrorExtension'] * @uses $GLOBALS['strUploadErrorUnknown'] * @uses $_FILES * @param string $key a numeric key used to identify the different rows * @param string $primary_key * @return boolean success */ function setUploadedFromTblChangeRequest($key, $primary = null) { if (! isset($_FILES['fields_upload_' . $key])) { return false; } $file = $_FILES['fields_upload_' . $key]; if (null !== $primary) { $file = PMA_File::fetchUploadedFromTblChangeRequestMultiple($file, $primary); } // rajk - for blobstreaming $is_bs_upload = FALSE; // check if this field requires a repository upload if (isset($_REQUEST['upload_blob_repo_' . $key])) $is_bs_upload = ($_REQUEST['upload_blob_repo_' . $key]['multi_edit'][0] == "on") ? TRUE : FALSE; // if request is an upload to the BLOB repository if ($is_bs_upload) { // load PMA configuration $PMA_Config = $_SESSION['PMA_Config']; // if PMA configuration is loaded if (!empty($PMA_Config)) { // load BS variables from PMA configuration $pluginsExist = $PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'); $curlExists = $PMA_Config->get('CURL_EXISTS'); $bs_database = $PMA_Config->get('BLOBSTREAMABLE_DATABASES'); $bs_database = $bs_database[$_REQUEST['db']]; $allBSTablesExist = TRUE; // determine if plugins and curl exist if ($pluginsExist && $curlExists) { foreach ($bs_database as $table_key=>$table) { if (!$bs_database[$table_key]['Exists']) { $allBSTablesExist = FALSE; break; } } } else $allBSTablesExist = FALSE; // if necessary BS tables exist if ($allBSTablesExist) { // setup bs variables for uploading $bs_server = $PMA_Config->get('BLOBSTREAMING_SERVER'); $bs_db = $_REQUEST['db']; $bs_table = $_REQUEST['table']; // setup file handle and related variables $tmp_file = fopen($file['tmp_name'], 'r'); $tmp_file_type = $file['type']; $tmp_file_size = $file['size']; if (!$tmp_file_type) $tmp_file_type = NULL; // if none of the required variables contain data, return with an unknown error message if (!$bs_server || !$bs_db || !$bs_table || !$tmp_file || !$tmp_file_size) { $this->_error_message = $GLOBALS['strUploadErrorUnknown']; return FALSE; } else $bs_server_path = 'http://' . $bs_server . '/' . $bs_db . '/' . $bs_table; // init curl handle $curlHnd = curl_init ($bs_server_path); // if curl handle init successful if ($curlHnd) { // specify custom header $customHeader = array( "Accept-Language: en-us;en;q=0;5", "Accept-Charset: ISO-8859-1;utf-8;q=0.7,*;q=0.7", "Content-type: $tmp_file_type" ); // specify CURL options in array $curlOptArr = array( CURLOPT_PUT => TRUE, CURLOPT_HEADER => TRUE, CURLOPT_HTTPHEADER => $customHeader, CURLOPT_INFILESIZE => $tmp_file_size, CURLOPT_INFILE => $tmp_file, CURLOPT_RETURNTRANSFER => TRUE ); // pass array of options to curl handle setup function curl_setopt_array($curlHnd, $curlOptArr); // execute curl request and retrieve error message(s) (if any) $ret = curl_exec($curlHnd); $errRet = curl_error($curlHnd); // close curl handle curl_close($curlHnd); // split entire string into array of lines $retArr = explode("\r\n", $ret); // check each line as a valid string of a BLOB reference foreach ($retArr as $value) if (strlen($value) > strlen("~*$bs_db/~") && "~*$bs_db/~" == substr($value, 0, strlen($bs_db) + 4)) { // is a valid reference, so set as current and break PMA_File::setRecentBLOBReference($value); break; } // close file handle if ($tmp_file) fclose($tmp_file); } // end if ($curlHnd) } // end if ($allBSTablesExist) } // end if ($PMA_Config) } // end if ($is_bs_upload) // check for file upload errors switch ($file['error']) { // cybot_tm: we do not use the PHP constants here cause not all constants // are defined in all versions of PHP - but the correct constants names // are given as comment case 0: //UPLOAD_ERR_OK: return $this->setUploadedFile($file['tmp_name']); break; case 4: //UPLOAD_ERR_NO_FILE: break; case 1: //UPLOAD_ERR_INI_SIZE: $this->_error_message = $GLOBALS['strUploadErrorIniSize']; break; case 2: //UPLOAD_ERR_FORM_SIZE: $this->_error_message = $GLOBALS['strUploadErrorFormSize']; break; case 3: //UPLOAD_ERR_PARTIAL:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -