php批量修改文件名程序

 更新时间:2016年11月25日 17:31  点击:2197
提供二款利用遍历批量修改文件的文件名的php代码,有需要的朋友可以参考一下。

实例一

 代码如下 复制代码
<?php
//利用PHP目录和文件函数遍历用户给出目录的所有的文件和文件夹,修改文件名称
function fRename($dirname){
 if(!is_dir($dirname)){
  echo "{$dirname}不是一个有效的目录!";
  exit();
 }
 $handle = opendir($dirname);
 while(($fn = readdir($handle))!==false){
  if($fn!='.'&&$fn!='..'){
   $curDir = $dirname.'/'.$fn;
   if(is_dir($curDir)){
    fRename($curDir);
   }
   else{
    $path = pathinfo($curDir);
    $newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];
    rename($curDir,$newname);   
    echo $curDir.'---'.$newname."<br>";   
   }
  }
 }
}
//给出一个目录名称调用函数
fRename('pl');
?>

实例二

 代码如下 复制代码

<?php
$dir = './';

if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
        if ($file == "." || $file == "..") continue;
        if(filetype($dir . $file) == 'file')
        {
            $newfile = str_replace('[1]', '', $file);
            rename($dir . $file, $dir . $newfile);
        }
    }
    closedir($dh);
}

文章也很简单我们只要用利用head 发送头部信息就能实现把文件下载到本了,有直接发送header信息也有把文件保存在服务器上再用header来发送哦,有需要的朋友参考下。

用的是表头方式下载

 代码如下 复制代码
Header( "Content-type:   application/octet-stream ");
Header( "Accept-Ranges:   bytes ");
Header( "Accept-Length:   ".filesize($file_dir   .   $file_name));
Header( "Content-Disposition:   attachment;   filename= "   .   $file_name);

判断文件存在再下载

 代码如下 复制代码
<?php
    if( isset( $_REQUEST["download"] ) )
    {
            $tfile = $_REQUEST["download"];
           
            if (file_exists($tfile))
            {
                $downfilename=substr($tfile,strrpos($tfile, "/")+1);
               //文件保存的名字可以修改为你需要的,可以和链接给的名字不一样
                // Prompt the user to download the new torrent file.
                header( "Content-type: application/octet-streamn" );
                header( "Content-disposition: attachment; filename=$downfilenamen" );
                header( "Content-transfer-encoding: binaryn");
                header( "Content-length: " . @filesize($tfile ) . "n" );
                // Send the torrent file
                $fp = @fopen( $tfile, "r" );
                @fpassthru( $fp );
                @fclose( $fp );
             }
           
            exit();
     }
?>

pdf文件下载

 代码如下 复制代码

<?php
$filename = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file/my_file.pdf";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Type: application/pdf"); //pdf格式的

?>


下面讲讲header中的Content-type:
不同的下载文件,对应不同的content-type,下面是大全:
PHP code
{
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk',
}

提供一个实例的php自定义函数,这是一个可以批量修改文件后缀名的php程序,有需要的朋友可以参考一下。
 代码如下 复制代码

<?php
function foreachDir($path){
$handle=opendir($path);
if($handle){
while (false !== ($file = readdir($handle))) {
if($file!="." && $file!='..'){
if(is_dir($path.$file)){
echo $path.$file."<br/>";
foreachDir($path.$file);
}else{
echo "--".$path."/".$file."<br/>";
$ext = strripos($file,'.');
$aaa = substr($file,0,$ext);
rename($path.'/'.$file,$path.'/'.$aaa.'.JPG');
// die();
}
}
}
return false;
}
}
foreachDir('D:xampphtdocsTNF2');
本文章给php初学者讲了两个php中实例的字符替换函数,一个是str_ireplace()一个是substr_replace()这两个函数都比较好用,有需要的参考一下。

字符串的替换技术可以通过以下两个常用函数实现:str_ireplace()函数和substr_replace()函数
str_ireplace()函数
使用新的子字符串替换原始字符串中被指定要替换的字符串,语法:
mixed str_ireplace(mixed search,mixed replace,mixed subject[,int&count])
参数search:必要参数,指定需要查找的字符串。
参数replace:必要参数,指定替换的值。
参数subject:必要参数,指定查找的范围。
参数count:可选参数,(带中括号的为可选参数),获取执行替换的数量。

实例:

 代码如下 复制代码
<?php
$str2=”某某”;
$str1=”**”;
$str=”某某网站的地址是www.111cn.net ,某某网站主要记录一些学习php的笔记和感想以及各种软件知识”;
echo str_ireplace($str2,$str1,$str);    //str2查找的值,str1替换的值,str范围
?>

在本例中,我们将演示带有数组和 count 变量的 str_ireplace() 函数:

 代码如下 复制代码

<?php
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("red","pink",$arr,$i));
echo "Replacements: $i";
?>

输出:

Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)

Replacements: 1例子 3

 代码如下 复制代码

<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>输出:

Array
(
[0] => B
[1] =>
[2] => !
)


 

substr_replace()函数
对指定字符串中的部分字符串进行替换,语法:
string substr_replace(string str,string repl,int start,[int length])
参数str:指定要操作的原始字符串。
参数repl:必要参数,指定替换后的新字符串。
参数start:指定替换字符串开始的位置。
参数length:指定返回的字符串长度。
实例:

 代码如下 复制代码

<?php
substr_replace('eggs','x',-1,-1); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
substr_replace('eggs','x',-1,-2); //eggxs
?> Same as:
<?php
substr_replace('eggs','x',-1,0); //eggxs
?>

<?php
substr_replace('huevos','x',-2,-2); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
substr_replace('huevos','x',-2,-3); //huevxos
?> Same as:

<?php
substr_replace('huevos','x',-2,0); //huevxos
?>

更多详细内容请查看:http://www.111cn.net/phper/21/32954.htm

文章简单的介绍了关于php音乐文件播放时间代码,有需要的朋友可以仔细的看看哦。 // mp3, wav 或者其他 media player 支持的格式.
 代码如下 复制代码
$file = "C:/Users/Public/Music/Sample Music/Kalimba.mp3";
 
if (!file_exists($file)) {
    exit('媒体文件不存在.');
}
 
// 创建一个Windows Media Player接口
$player = new COM("WMPlayer.OCX");
$media  = $player->newMedia($file);
 
// 获取歌曲时间 (单位秒)
echo $media->duration.'秒';
[!--infotagslink--]

相关文章

  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • cmd下过滤文件名称的两种方法

    这篇文章主要介绍了cmd下过滤文件名称的两种方法,需要的朋友可以参考下...2020-06-30
  • C#使用Process类调用外部exe程序

    本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文

    时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • Python爬取微信小程序通用方法代码实例详解

    这篇文章主要介绍了Python爬取微信小程序通用方法代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-29
  • 微信小程序(应用号)开发新闻客户端实例

    这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25
  • 微信小程序手势操作之单触摸点与多触摸点

    这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
  • 使用phpMyAdmin批量修改Mysql数据表前缀的方法

    多个网站共用一个Mysql数据库时,为使数据库管理不混乱,一般采用不同的网站使用不同前缀名的方式进行区分。而如何批量修改已有数据库的前缀名呢?全部导出修改后再导入?还是一个表一个表的修改?今天我要介绍的是相对简单的...2015-10-21
  • 手把手教你uniapp和小程序分包(图文)

    本文主要介绍了手把手教你uniapp和小程序分包,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-02
  • 微信小程序 页面跳转传递值几种方法详解

    这篇文章主要介绍了微信小程序 页面跳转传递值几种方法详解的相关资料,需要的朋友可以参考下...2017-01-16
  • 微信小程序实现canvas分享朋友圈海报

    这篇文章主要为大家详细介绍了微信小程序实现canvas分享朋友圈海报,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21