博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php s3扩展文件系统,php - PHP:获取文件扩展名不能上传到S3 - 堆栈内存溢出
阅读量:7000 次
发布时间:2019-06-27

本文共 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/

你可能感兴趣的文章
Filter技术+职责链模式
查看>>
HTML学习笔记(七)
查看>>
HexColor
查看>>
专业缩写积累
查看>>
GNU GPL介绍
查看>>
Oracle段收缩功能
查看>>
UVALive 4222 Dance 模拟题
查看>>
汇编题目:在DOS下,按F1键后改变当前屏幕的显示颜色
查看>>
谁是Docker的开发人员
查看>>
什么是REST API?
查看>>
Facebook存储技术方案:找出“暖性BLOB”数据
查看>>
Windows环境下载与安装JBOSS服务器的详细图文教程
查看>>
深入了解STL中set与hash_set,hash表基础
查看>>
python使用VBA:Excel创建图表(转)
查看>>
InnoDB的WAL方式学习
查看>>
Asp.net MVC Comet推送
查看>>
Git的冲突解决过程
查看>>
debug 输出 以及宏定义--备
查看>>
2016年某前端群题目答案参考
查看>>
mysql将字符转换成数字
查看>>