php 源代码上传,php文件上传经典代码

这段代码是一个PHP函数,用于处理文件上传。它首先检查文件是否已上传,然后验证文件扩展名是否允许,并进行安全检查,如图片合法性验证和HTML内容检测。如果文件存在则移动到指定目录,并调整权限。如果上传过程中出现错误,将返回相应的错误代码。
摘要由CSDN通过智能技术生成

php文件上传经典代码

function FileUpload( $resourceType, $currentFolder, $sCommand )

{

if (!isset($_FILES)) {

global $_FILES;

}

$sErrorNumber = '0' ;

$sFileName = '' ;

if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) )

{

global $Config ;

$oFile = $_FILES['NewFile'] ;

// Map the virtual path to the local server path.

$sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ;

// Get the uploaded file name.

$sFileName = $oFile['name'] ;

$sFileName = SanitizeFileName( $sFileName ) ;

$sOriginalFileName = $sFileName ;

// Get the extension.

$sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;

$sExtension = strtolower( $sExtension ) ;

if ( isset( $Config['SecureImageUploads'] ) )

{

if ( ( $isImageValid = IsImageValid( $oFile['tmp_name'], $sExtension ) ) === false )

{

$sErrorNumber = '202' ;

}

}

if ( isset( $Config['HtmlExtensions'] ) )

{

if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) &&

( $detectHtml = DetectHtml( $oFile['tmp_name'] ) ) === true )

{

$sErrorNumber = '202' ;

}

}

// Check if it is an allowed extension.

if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) )

{

$iCounter = 0 ;

while ( true )

{

$sFilePath = $sServerDir . $sFileName ;

if ( is_file( $sFilePath ) )

{

$iCounter++ ;

$sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;

$sErrorNumber = '201' ;

}

else

{

move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;

if ( is_file( $sFilePath ) )

{

if ( isset( $Config['ChmodOnUpload'] ) && !$Config['ChmodOnUpload'] )

{

break ;

}

$permissions = 0777;

if ( isset( $Config['ChmodOnUpload'] ) && $Config['ChmodOnUpload'] )

{

$permissions = $Config['ChmodOnUpload'] ;

}

$oldumask = umask(0) ;

chmod( $sFilePath, $permissions ) ;

umask( $oldumask ) ;

}

break ;

}

}

if ( file_exists( $sFilePath ) )

{

//previous checks failed, try once again

if ( isset( $isImageValid ) && $isImageValid === -1 && IsImageValid( $sFilePath, $sExtension ) === false )

{

@unlink( $sFilePath ) ;

$sErrorNumber = '202' ;

}

else if ( isset( $detectHtml ) && $detectHtml === -1 && DetectHtml( $sFilePath ) === true )

{

@unlink( $sFilePath ) ;

$sErrorNumber = '202' ;

}

}

}

else

$sErrorNumber = '202' ;

}

else

$sErrorNumber = '202' ;

$sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ;

$sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ;

SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ;

exit ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值