dongyun9120 2013-05-09 15:02
浏览 42
已采纳

使用DirectoryIterator循环遍历D:驱动器目录

I am trying to loop through this directory:

$path = "D:\\import\\statsummary\\";

Here is my code:

$path = "D:\\import\\statsummary\\";
//$path = "C:\\test";
//function load_csv($path, $filename){
    if(is_null($filename)){
        header('Content-type: text/plain');
        $output = array();
        foreach (new DirectoryIterator($path) as $file){
            if($file->isFile()){
                $output[] = $i++ . " " . $file->getFileName() . "
";
                $output[] = file($file->getPathName());
                $output[] = "
------------
";
            }
        }
    }

    echo implode('', $output);

When I run this script, I get this error:

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(D:\import\statsummary\,D:\import\statsummary\): Access is denied. (code: 5)' in C:\inetpub\wwwroot\include\file_importer.php:10
Stack trace:
#0 C:\inetpub\wwwroot\include\file_importer.php(10): DirectoryIterator->__construct('D:\import\...')
#1 {main}
  thrown in C:\inetpub\wwwroot\include\file_importer.php on line 10

But when I change it to a test directory on my C:\ drive, it runs just fine. I've even created a username to run PHP as directed in this post:

php - Unable to connect to network share - Stack Overflow

  • 写回答

1条回答 默认 最新

  • draw62188 2013-05-09 15:16
    关注

    Based on the DirectoryIterator class, something like this should work:

    <?php
    $path = "D:/import/statsummary";
    $output=array();
    $iterator = new DirectoryIterator(path);
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            $filename= $fileinfo->getFilename();
            $path=$fileinfo->getPathname();
            $output[][$filename]=$path;
        }
    }
    
    print_r($output);
    ?>
    

    Update

    Since you're getting access denied, you'll need to run the command prompt (CMD) window as Administrator more than likely. If this is on a link (lnk) you can change the permissions in the link settings.

    For instance if you right-click on the shortcut for cmd as select properties, you would go to shortcut>advanced>Run as Administrator.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP