设为首页收藏本站
查看: 162|回复: 0

[PHP] php文件上传经典代码-PHP源码

[复制链接]

论坛元老

Rank: 6Rank: 6

积分
34274
主题
17031
UID
1347
M币
67
贡献
17176

  • 发表于 2018-7-16 23:57:00 | 显示全部楼层 |阅读模式
    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 ;
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    在我站开通SVIP可同时获得17个站点VIP资源 立即登录 立即注册
    快速回复 返回顶部 返回列表