doushu8260 2014-03-27 15:27
浏览 63
已采纳

PHP脚本完成后删除文件

I have the following php file which executes with a cron job. How can I add the functionality that once completed, the xml files in the directory get deleted.

<?php 

// Create connection
$con=mysqli_connect("localhost","test","test","epg");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$dir = "xml-files/";
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if (($file !== '.') && ($file !== '..') ) {
    $doc = simplexml_load_file($dir . $file); 

foreach ( $doc->ScheduleData->ChannelPeriod as $channelPeriod )
{
   $channelId = $channelPeriod->ChannelId;

   foreach ( $channelPeriod->Event as $event )
   {
      $beginTime = $event['beginTime'];
      $duration = $event['duration'];
      $programName = $event->EpgProduction->EpgText->Name;
      $description = $event->EpgProduction->EpgText->Description;
      $EventId = $event->EventId;

      $sql = "insert into `epg` (`EventId`,`ChannelId`, `BeginTime`,`Duration`, `ShortName`, `Description`) values ('$EventId','$channelId', '$beginTime','$duration', '$programName', '$description')";

        if (mysqli_query($con,$sql))
      {
      echo "Database updated successfully";
      }
    else
      {
      echo "Error creating database: " . mysqli_error($con);
      }

   }
}
}
  }
  closedir($dh);
  }
}
?>

Many thanks for your help guys! much appreciated.

  • 写回答

4条回答 默认 最新

  • dseslyh6662605 2014-03-27 15:31
    关注

    At the end of your code;

    function deleteFiles($dir) {
        $files = glob($dir);
        foreach($files as $file){ 
          if(is_file($file))
            unlink($file); 
        }
    }
    
    deleteFiles("xml-files/*"); This will delete all files under this dir
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效