国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

wordpress文件上傳到服務(wù)器改變文件名

2024-09-07 00:50:28
字體:
供稿:網(wǎng)友

沒有人能保證自己上傳的文件時不會存在相同名字的,那么要如何解決此問題呢,wordpress博客中我們可以利用wp_handle_upload_prefilter來解決此問題,下面演示一個實例.

例,利用圖片高與寬生+文件名成名字,代碼如下:

  1. add_filter( 'wp_handle_upload_prefilter''modify_uploaded_file_names', 20); 
  2. function modify_uploaded_file_names( $image ) { 
  3.     // Get default name of uploaded file and set to variable 
  4.     $imagename = $image['name']; 
  5.     // Case switch for multiple file extensions 
  6.     switch ( $image['type'] ) { 
  7.         case 'image/jpeg' : 
  8.             $suffix = 'jpg'
  9.             break
  10.         case 'image/png' : 
  11.             $suffix = 'png'
  12.             break
  13.         case 'image/gif' : 
  14.             $suffix = 'gif'
  15.             break
  16.     } 
  17.     // Get size of uploaded image and assign to variable 
  18.     $imagesize = getimagesize($image); 
  19.     // Re-structure uploaded image name 
  20.     $image['name'] = "{$imagesize[0]}x{$imagesize[1]}-{$imagename}.{$suffix}"
  21.  
  22.     return $image

例,利用年月日時分秒+千位毫秒整數(shù)

以wordpress 3.2.1為例,打開“wp-admin/includes/file.php” www.survivalescaperooms.com文件,找到第327行這段代碼:

  1. // Move the file to the uploads dir 
  2. $new_file = $uploads['path'] . "/$filename"
  3. if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) 
  4.         return $upload_error_handler$file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) ); 

將其修改為如下代碼:

  1. // Move the file to the uploads dir 
  2. $new_file = $uploads['path'] . "/".date("YmdHis").floor(microtime()*1000).".".$ext;  
  3. if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) 
  4.         return $upload_error_handler$file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) ); 

保存,重新上傳文件,這樣,新上傳的文件,就會自動保存為“年月日時分秒+千位毫秒整數(shù)”的新文件名,并保存到相應(yīng)的年月文件夾之下了.

提醒你,這兩種方法個人覺得后者更適合我們一些哦,因為按年月日時分秒+千位毫秒整數(shù)不會出現(xiàn)重復(fù)名字,而按圖片高與寬生+文件名成名字還有可能存在重復(fù)名字.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石景山区| 清涧县| 昂仁县| 哈尔滨市| 门头沟区| 叙永县| 沅江市| 东丰县| 香格里拉县| 鹤山市| 博野县| 乳山市| 连城县| 乐至县| 马尔康县| 阜城县| 牟定县| 潮州市| 铜川市| 上杭县| 子洲县| 喀喇沁旗| 东乡| 丹江口市| 神农架林区| 革吉县| 三穗县| 岳阳县| 伊金霍洛旗| 朝阳区| 东山县| 潮州市| 梅州市| 秭归县| 苍梧县| 枣庄市| 依兰县| 公主岭市| 宜昌市| 志丹县| 滦南县|