php创造文件夹函数,上传图片扩张水印代码修改

作者: 计算机网络技术  发布:2019-09-28

php创建文件夹函数

function getfolders( $resourcetype, $currentfolder )
{
 // map the virtual path to the local server path.
 $sserverdir = servermapfolder( $resourcetype, $currentfolder, 'getfolders' ) ;

function CreateFolder( $resourceType, $currentFolder )
{
 if (!isset($_GET)) {
  global $_GET;
 }
 $sErrorNumber = '0' ;
 $sErrorMsg  = '' ;

 // array that will hold the folders names.
 $afolders = array() ;

 if ( isset( $_GET['NewFolderName'] ) )
 {
  $sNewFolderName = $_GET['NewFolderName'] ;
  $sNewFolderName = SanitizeFolderName( $sNewFolderName ) ;

 $ocurrentfolder = opendir( $sserverdir ) ;

  if ( strpos( $sNewFolderName, '..' ) !== FALSE )
   $sErrorNumber = '102' ;  // Invalid folder name.
  else
  {
   // Map the virtual path to the local server path of the current folder.
   $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'CreateFolder' ) ;

 while ( $sfile = readdir( $ocurrentfolder ) )
 {
  if ( $sfile != '.' && $sfile != '..' && is_dir( $sserverdir . $sfile ) )
   $afolders[] = '<folder name="' . converttoxmlattribute( $sfile ) . '" />' ;
 }

   if ( is_writable( $sServerDir ) )
   {
    $sServerDir .= $sNewFolderName ;

 closedir( $ocurrentfolder ) ;

    $sErrorMsg = CreateServerFolder( $sServerDir ) ;

 // open the "folders" node.
 echo "<folders>" ;

    switch ( $sErrorMsg )
    {
     case '' :
      $sErrorNumber = '0' ;
      break ;
     case 'Invalid argument' :
     case 'No such file or directory' :
      $sErrorNumber = '102' ;  // Path too long.
      break ;
     default :
      $sErrorNumber = '110' ;
      break ;
    }
   }
   else
    $sErrorNumber = '103' ;
  }
 }
 else
  $sErrorNumber = '102' ;

 natcasesort( $afolders ) ;
 foreach ( $afolders as $sfolder )
  echo $sfolder ;

 // Create the "Error" node.
 echo '<Error number="' . $sErrorNumber . '" originalDescription="' . ConvertToXmlAttribute( $sErrorMsg ) . '" />' ;
}

 // close the "folders" node.
 echo "</folders>" ;
}

function getfoldersandfiles( $resourcetype, $currentfolder )
{
 // map the virtual path to the local server path.
 $sserverdir = servermapfolder( $resourcetype, $currentfolder, 'getfoldersandfiles' ) ;

 // arrays that will hold the folders and files names.
 $afolders = array() ;
 $afiles  = array() ;

 $ocurrentfolder = opendir( $sserverdir ) ;

 while ( $sfile = readdir( $ocurrentfolder ) )
 {
  if ( $sfile != '.' && $sfile != '..' )
  {
   if ( is_dir( $sserverdir . $sfile ) )
    $afolders[] = '<folder name="' . converttoxmlattribute( $sfile ) . '" />' ;
   else
   {
    $ifilesize = @filesize( $sserverdir . $sfile ) ;
    if ( !$ifilesize ) {
     $ifilesize = 0 ;
    }
    if ( $ifilesize > 0 )
    {
     $ifilesize = round( $ifilesize / 1024 ) ;
     if ( $ifilesize < 1 ) $ifilesize = 1 ;
    }

    $afiles[] = '<file name="' . converttoxmlattribute( $sfile ) . '" size="' . $ifilesize . '" />' ;
   }
  }
 }

 // send the folders
 natcasesort( $afolders ) ;
 echo '<folders>' ;

 foreach ( $afolders as $sfolder )
  echo $sfolder ;

 echo '</folders>' ;

 // send the files
 natcasesort( $afiles ) ;
 echo '<files>' ;

 foreach ( $afiles as $sfiles )
  echo $sfiles ;

 echo '</files>' ;
}

function createfolder( $resourcetype, $currentfolder )
{
 if (!isset($_get)) {
  global $_get;
 }
 $serrornumber = '0' ;
 $serrormsg  = '' ;

 if ( isset( $_get['newfoldername'] ) )
 {
  $snewfoldername = $_get['newfoldername'] ;
  $snewfoldername = sanitizefoldername( $snewfoldername ) ;

  if ( strpos( $snewfoldername, '..' ) !== false )
   $serrornumber = '102' ;  // invalid folder name.
  else
  {
   // map the virtual path to the local server path of the current folder.
   $sserverdir = servermapfolder( $resourcetype, $currentfolder, 'createfolder' ) ;

   if ( is_writable( $sserverdir ) )
   {
    $sserverdir .= $snewfoldername ;

    $serrormsg = createserverfolder( $sserverdir ) ;

    switch ( $serrormsg )
    {
     case '' :
      $serrornumber = '0' ;
      break ;
     case 'invalid argument' :
     case 'no such file or directory' :
      $serrornumber = '102' ;  // path too long.
      break ;
     default :
      $serrornumber = '110' ;
      break ;
    }
   }
   else
    $serrornumber = '103' ;
  }
 }
 else
  $serrornumber = '102' ;

 // create the "error" node.
 echo '<error number="' . $serrornumber . '" originaldescription="' . converttoxmlattribute( $serrormsg ) . '" />' ;
}

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 ;
    //$sfilepath =$sserverdir.time().$sextension;     

    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 ;
    }
   }       

 代码如下

 //增加水印
   // add start
   
    
   $waterimg="111cn.net.png";    //水印图片  这里修改你的水印图片所在的地址
    
   if($watermark==1)
   {
    $image_size = getimagesize($sfilepath);
    $iinfo=getimagesize($sfilepath,$iinfo);
    $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
    $white=imagecolorallocate($nimage,255,255,255);
    $black=imagecolorallocate($nimage,0,0,0);
    $red=imagecolorallocate($nimage,255,0,0);
    imagefill($nimage,0,0,$white);
    switch ($iinfo[2])
    {
     case 1:
      $simage =imagecreatefromgif($sfilepath);
      break;
     case 2:
      $simage =imagecreatefromjpeg($sfilepath);
      break;
     case 3:
      $simage =imagecreatefrompng($sfilepath);
      break;
   //            case 6:
   //            $simage =imagecreatefromwbmp($sfilepath);
   //            break;
     default:
      die("不支持的文件类型");
      exit;
    }
    
    imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
    
    switch($watertype)
    {
     case 1:   //加水印字符串
      imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
      break;
     case 2:   //加水印图片
      $simage1 =imagecreatefrompng($waterimg);
      imagecopy($nimage,$simage1,$image_size[0]/2,$image_size[0]/2,0,0,240,65);
      imagedestroy($simage1);
      break;
    }
         
    switch ($iinfo[2])
    {
     case 1:
      imagegif($nimage, $sfilepath);
   //            imagejpeg($nimage, $sfilepath);
      break;
     case 2:
      imagejpeg($nimage, $sfilepath);
      break;
     case 3:
      imagepng($nimage, $sfilepath);
      break;
   //            case 6:
   //            imagewbmp($nimage, $sfilepath);
   //            //imagejpeg($nimage, $sfilepath);
   //            break;
    }
    
    //覆盖原上传文件
    imagedestroy($nimage);
    imagedestroy($simage);
   }

  //增加水印结果
   
   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 ;
}?>

本文由今晚买四不像发布于计算机网络技术,转载请注明出处:php创造文件夹函数,上传图片扩张水印代码修改

关键词: