使用 PHP 访问 Windows 对象? [英] Accessing Windows Objects with PHP?

查看:29
本文介绍了使用 PHP 访问 Windows 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个以特定方式组织文件内容的工具.这些文件遍布我在 Windows 7 上运行的电脑.该工具由两部分组成: 1 包含表单的界面.2 脚本做的工作.

与其手动将某个目录的完整路径写入主脚本,我宁愿让工具搜索它并无缝检索它.我正在考虑添加一个文本字段和一个按钮,在其中我可以输入我要查找的目录名称,然后单击按钮后,检索目录的完整路径名,将其打印到相同的文本字段,然后将其传递给程序本身.

我已经搜索了几天让 PHP 与 Windows 交互的方法(可能与窗口的搜索对象),但我发现的只是关于 COM 和 NET 的非常长的文档.然而,这些似乎严格处理访问 Office 对象,因为大多数可用示例都是关于 Excel 或 Word 对象的.

如何完成我想添加到界面中的功能?

为了避免进一步混淆,这是我所指的 Window 对象的图像 >

另一个例子:

$arr = preg_find('/\.tmp$/i','z:\temp',PREG_FIND_RECURSIVE | PREG_FIND_DIRMATCH);var_dump($arr);

I made a tool to organize file content in a specific way. These files are located all over my pc which runs on Windows 7. The tool is made up of two parts: 1 the interface holding a form. 2 the script to do the work.

Instead of having to manually write the full path to a certain directory to the main script, I'd rather have the tool search for it and retrieve it seamlessly. I'm thinking of maybe adding a textfield and a button, in which I can enter the directory's name I'm looking for and after clicking the button, retrieve the directory's full pathname, print it to the same textfield and then pass it along to the program itself.

I've searched for several day for ways to have PHP interact with Windows (maybe with window's search object), but all I've found is very looong documentation on the COM and then on the NET. These however seem to strictly deal with accessing Office objects, since most of the available examples are about Excel or Word objects.

How can I accomplish the functionality I want to add to my interface?

To avoid further confusion, this is the image of the Window's object I'm referring to > Windows Starup Search Field

解决方案

Use this handy function - just point it to the root of your filesystem and it will return an array with all the matching files - I mean, matched by the regular expression pattern you provide to the function.

// PREG_FIND_RECURSIVE   - go into subdirectorys looking for more files
// PREG_FIND_DIRMATCH    - return directorys that match the pattern also
// PREG_FIND_DIRONLY     - return only directorys that match the pattern (no files)
// PREG_FIND_FULLPATH    - search for the pattern in the full path (dir+file)
// PREG_FIND_NEGATE      - return files that don't match the pattern
// PREG_FIND_RETURNASSOC - Instead of just returning a plain array of matches,
//                         return an associative array with file stats
// to use more than one simply seperate them with a | character

define('PREG_FIND_RECURSIVE', 1);
define('PREG_FIND_DIRMATCH', 2);
define('PREG_FIND_FULLPATH', 4);
define('PREG_FIND_NEGATE', 8);
define('PREG_FIND_DIRONLY', 16);
define('PREG_FIND_RETURNASSOC', 32);

function preg_find($pattern, $start_dir='.', $args=NULL)
{
  $files_matched = array();
  $fh = @opendir($start_dir);
  if($fh)
  {
    while (($file = readdir($fh)) !== false)
      {
        if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
        $filepath = $start_dir . '/' . $file;
      if (preg_match($pattern, ($args & PREG_FIND_FULLPATH) ? $filepath : $file))
        {
          $doadd =     is_file($filepath)
                   || (is_dir($filepath) && ($args & PREG_FIND_DIRMATCH))
                   || (is_dir($filepath) && ($args & PREG_FIND_DIRONLY));
          if ($args & PREG_FIND_DIRONLY && $doadd && !is_dir($filepath)) $doadd = false;
          if ($args & PREG_FIND_NEGATE) $doadd = !$doadd;
        if ($doadd)
          {
            if ($args & PREG_FIND_RETURNASSOC) // return more than just the filenames
            {
              $fileres = array();
            if (function_exists('stat'))
              {
                $fileres['stat'] = stat($filepath);
                $fileres['du'] = $fileres['stat']['blocks'] * 512;
              }
              //if (function_exists('fileowner')) $fileres['uid'] = fileowner($filepath);
              //if (function_exists('filegroup')) $fileres['gid'] = filegroup($filepath);
              //if (function_exists('filetype')) $fileres['filetype'] = filetype($filepath);
              //if (function_exists('mime_content_type')) $fileres['mimetype'] = mime_content_type($filepath);
              if (function_exists('dirname')) $fileres['dirname'] = dirname($filepath);
              if (function_exists('basename')) $fileres['basename'] = basename($filepath);
              //if (isset($fileres['uid']) && function_exists('posix_getpwuid ')) $fileres['owner'] = posix_getpwuid ($fileres['uid']);
              $files_matched[$filepath] = $fileres;
          }
            else array_push($files_matched, $filepath);
          }
        }
        if ( is_dir($filepath) && ($args & PREG_FIND_RECURSIVE) ) $files_matched = array_merge($files_matched, preg_find($pattern, $filepath, $args));
      }
    closedir($fh);
    }
  return $files_matched;
}

Example usage:

$arr = preg_find('/./','z:\temp');
var_dump($arr);

Example output:

Another example:

$arr = preg_find('/\.tmp$/i','z:\temp',PREG_FIND_RECURSIVE | PREG_FIND_DIRMATCH);
var_dump($arr);

这篇关于使用 PHP 访问 Windows 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆