Home > php教程 > php手册 > body text

php工具:搜索文件并执行系统命令

WBOY
Release: 2016-06-06 19:35:46
Original
1495 people have browsed it

1.匹配指定的关键字查找文件 2.匹配到文件后再执行相应的系统命令或生成批处理文件 3.反馈执行结果 4.示例:$phpfind.phpd:/photo.jpg,.gif,.pnggmconvert-resize200X200$filename$filename 5.示例解释:查找d:/photo下的所有文件夹及子文件夹jpg,gif,png格

1.匹配指定的关键字查找文件
2.匹配到文件后再执行相应的系统命令或生成批处理文件
3.反馈执行结果
4.示例:$php find.php d:/photo .jpg,.gif,.png "gm convert -resize 200X200 $filename $filename"
5.示例解释:查找d:/photo下的所有文件夹及子文件夹jpg,gif,png格式,调用GraphicsMagick工具转换尺寸为200*200,并覆盖原来的文件
<?php

/***
    Name: find file
    Example:
        php find.php d:/photo .jpg,.gif,.png "gm convert -resize 100X100 $filename $filename"
        php find.php .txt,.doc "copy $filename e:/document"
***/

// create bat file? 1:yes, 0:no
define('CREATE_BAT_FILE', 0);

switch(count($argv)) {
    case 4:
        $command = $argv[3];
        $match = $argv[2];
        $dir = $argv[1];
        break;
    case 3:
        $command = $argv[2];
        $match = $argv[1];
        $dir = __DIR__;
        break;
    default:
        exit('Error: Missing parameters!' . PHP_EOL . 
            'Example: path match command, d:/dir .jpg,.gif "echo $name"' . PHP_EOL);
}

if(!is_dir($dir)) {
    exit($dir . ' not a directory.' . PHP_EOL);
}

$directory = array(str_replace('\\', '/', $dir));
$files = array();
$index = 0;
$count = 0;
$result = '';

while($currentPath = current($directory)) {

    $dirHandle = dir($currentPath);
    
    while(false !== ($name = $dirHandle->read())) {
        
        if($currentPath[strlen($currentPath) - 1] == '/') {
            $filename = $currentPath . $name;
        } else {
            $filename = $currentPath . '/' . $name;
        }

        if($name == '..' || $name == '.') {
            continue;
        }
        if(is_dir($filename)) {
            $directory[] = $filename;
        } else {
            str_replace(explode(',', $match), '', $name, $count);
            if($match != '*' && $count == 0) {
                continue;
            }
            $template = array('$name', '$filename', '$path', '$index');
            $variable = array($name, $filename, $currentPath, $index);
            $cmd = str_replace($template, $variable, $command);
            if(CREATE_BAT_FILE) {
                $result .= $cmd . PHP_EOL;
            } else {
                echo shell_exec($cmd);
            }
            $index++;
        }
    }

    next($directory);
}

// create bat file
if(CREATE_BAT_FILE) {
    $batFile = fopen('temp.bat', 'w');
    fwrite($batFile, $result);
    fclose($batFile);
    echo 'output file to: ' . str_replace('\\', '/', __DIR__) . '/temp.bat' . PHP_EOL;
}

// echo result:
echo $index . ' file find.' . PHP_EOL;
Copy after login
php工具:搜索文件并执行系统命令
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template