douli0531 2013-06-06 21:32
浏览 13

尝试将目录中的文件读入PHP脚本

I've been trying to get my php script to read a directory for specific files and run the script on each, but I can't get it to work. I keep getting an error failed to open directory. I'm wondering if there is a small mistake I've made, and I've been struggling trying to figure out why it won't read the files.

<?php
$bg = "bg-body.png";
?>

<html>
<style type="text/css">
body {
background-image: url('<?php echo $bg;?>');
background-repeat: repeat;
background-position: top center;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Movie List</title>
</html>


<?php

$file_list = array();
$file_folder = "C:\Users\Looper\Documents";

//use the directory class
$files = dir($file_folder);

//read all files from the  directory
while ($file = $files->read()) {
   chdir($file_folder);
  $files = glob('*.mov');
  foreach ($files as $file) {
    $file_list[] = $file;
   } 
}
closedir($files->handle);

//display image
foreach($file_list as $file) {
    $theData = file_get_contents($file) or die("Unable to retrieve file data");
}

$months = ['January' => '_01', 'February' =>  '_02', 'March' => '_03', 'April' => '_04', 'May' => '_05', 'June' => '_06', 'July' => '_07', 'August' => '_08', 'September' => '_09', 'October' => '_10', 'November' => '_11', 'December' => '_12'];
foreach($months as $key => $month){
  if(strpos($file,$month)!==false){
        echo "<div style ='text-align: center; text-shadow: 0 .8px 0 #c4bc2a; margin-top: 30px; margin-bottom: 20px; font:16px verdana,tahoma,sans-serif;
                color:#6b8942; font-weight:bold; text-decoration: underline;'>Movie List for $key 2013</div>";
    }
}


$string = $theData;
$titles = explode("
", $string);

function getInfo($string){
    $Ratings = ['G', 'PG', 'PG-13', 'R', 'NR', 'XXX'];
    $split = preg_split("/\"(.+)\"/", $string, 0, PREG_SPLIT_DELIM_CAPTURE); 
    if(count($split) == 3){ 
        preg_match("/(".implode("|", $Ratings).")\s/", $split[0], $matches);
        $rating = $matches[0];
        return ["title" => $split[1], "rating" => $rating];
    }
    return false;
}


$infolist = array();
foreach($titles as $title){
    $info = getInfo($title);
    if($info !== false){
    $infolist[] = $info;
    }
}

usort($infolist, "infosort");

function infosort($lhs,$rhs) {
  return strcmp($lhs['rating'], $rhs['rating']);
}

foreach ($infolist as $info) {
        echo "<div style ='margin-bottom: 3px; text-align: center;
          font:13px Verdana,tahoma,sans-serif;color:green;'> 
           {$info["title"]} : {$info["rating"]}</div>";
}

echo "<div style='text-align:center; margin-top: 20px;'><img src='shclogo.png'
alt='Logo' width='200' height='133'/></div>";

?>
  • 写回答

1条回答 默认 最新

  • dtmsaqtly798322992 2013-06-06 21:35
    关注

    "C:\Users\Looper\Documents" is not a valid path. "C:\\Users\\Looper\\Documents" might be..

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害