本文共 3864 字,大约阅读时间需要 12 分钟。
重命名文件并计算扩展名的简单解决方案 :
$fileName = 'myRandomFile.jpg';
// separate the '.'-separated parts of the file name
$parts = explode( '.', $fileName );
// Solution will not work, if no extension is present
assert( 1 < count( $parts ) );
// keep the extension and drop the last part
$extension = $parts[ count( $parts ) - 1 ];
unset( $parts[ count( $parts ) - 1 ] );
// finally, form the new file name
$newFileName = md5( 'someSeedHere' + implode( '.', $parts )) . '.' . $extension;
echo $extension // outputs jpg
. ' - '
. $newFileName // outputs cfcd208495d565ef66e7dff9f98764da.jpg
;
注意, md5()总是32字节长,并且对于计算值不唯一 。 对于许多实际情况,它足够独特 。
附录
此外,您可以使用此解决方案来跟踪变量更改 :
abstract class CSTReportDelegate {
abstract public function emitVariableChange( $variableName, $oldValue, $newValue );
abstract public function emitVariableSetNew( $variableName, $newValue );
}
class CSTSimpleReportDelegate extends CSTReportDelegate {
public function emitVariableChange( $variableName, $oldValue, $newValue ) {
echo '
[global/change] '. $variableName . ' : ' . print_r( $oldValue, true ) . ' → ' . print_r( $newValue, true );}
public function emitVariableSetNew( $variableName, $newValue ) {
echo '
[global/init] '. $variableName . ' → ' . print_r( $newValue, TRUE );}
}
class CSysTracer {
static protected
$reportDelegate;
static private
$globalState = array();
static private
$traceableGlobals = array();
static private
$globalTraceEnabled = FALSE;
const
DEFAULT_TICK_AMOUNT = 1;
static public
function setReportDelegate( CSTReportDelegate $aDelegate ) {
self::$reportDelegate = $aDelegate;
}
static public
function start( $tickAmount = self::DEFAULT_TICK_AMOUNT ) {
register_tick_function ( array( 'CSysTracer', 'handleTick' ) );
}
static public
function stop() {
unregister_tick_function( array( 'CSysTracer', 'handleTick' ) );
}
static public
function evalAndTrace( $someStatement ) {
declare( ticks = 1 ); {
self::start();
eval( $someStatement );
self::stop();
}
}
static public
function addTraceableGlobal( $varName ) {
if ( is_array( $varName )) {
foreach( $varName as $singleName ) {
self::addTraceableGlobal( $singleName );
}
return;
}
self::$traceableGlobals[ $varName ] = $varName;
}
static public
function removeTraceableGlobal( $varName ) {
unset( self::$traceableGlobals[ $varName ] );
}
/**
* Main function called at each tick. Calls those functions, which
* really perform the checks.
*
*/
static public
function handleTick( ) {
if ( TRUE === self::$globalTraceEnabled ) {
self::traceGlobalVariable();
}
}
static public
function enableGlobalsTrace() {
self::$globalTraceEnabled = TRUE;
}
static public
function disableGlobalsTrace() {
self::$globalTraceEnabled = FALSE;
}
static public
function traceGlobalVariable( ) {
foreach( self::$traceableGlobals as $aVarname ) {
if ( ! isset( $GLOBALS[ $aVarname ] )) {
continue;
}
if ( ! isset( self::$globalState[ $aVarname ] ) ) {
self::$reportDelegate->emitVariableSetNew( $aVarname, $GLOBALS[ $aVarname ] );
self::$globalState[ $aVarname ] = $GLOBALS[ $aVarname ];
continue;
}
if ( self::$globalState[ $aVarname ] !== $GLOBALS[ $aVarname ]) {
self::$reportDelegate->emitVariableChange( $aVarname, self::$globalState[ $aVarname ], $GLOBALS[ $aVarname ] );
}
self::$globalState[ $aVarname ] = $GLOBALS[ $aVarname ];
}
}
}
示例用例:
ini_set("display_errors", TRUE);
error_reporting(E_ALL);
require_once( dirname( __FILE__ ) . '/CStatementTracer.inc.php' );
/* Ticks make it easy to have a function called for every line of PHP
* code. We can use this to track the state of a variable throughout
* the execution of a script.
*/
CSysTracer::addTraceableGlobal( array( 'foo', 'bar' ));
CSysTracer::setReportDelegate( new CSTSimpleReportDelegate() );
CSysTracer::enableGlobalsTrace();
CSysTracer::start();
declare( ticks = 1 );
//
// At this point, tracing is enabled.
// Add your code or call your functions/methods here
//
CSysTracer::stop();
转载地址:http://xdevl.baihongyu.com/