繁体   English   中英

php 列出目录中的文件并保存到磁盘

[英]php list files in directory and save to disk

我想列出“archives/”中的文件并将解码后的数组写入磁盘(文件名=“archives/events.txt”)但我所拥有的并没有创建该文件。

xhr.send(数据); // 给出: Uncaught ReferenceError: data is not defined

我究竟做错了什么?

function LISTevents () {
var xhr = new XMLHttpRequest();
 xhr.open("POST", "json-events.php");
 xhr.onload = function () {
 console.log(this.response);
 };
 xhr.send(data);
 return false;
}

json-events.php

<?php
// create list of events files in archives/* on server

$files1 = scandir('archives');

file_put_contents('archives/events.txt', print_r($files1, true));

?>
<?php
function list_contents($dir, $suffix) {
 $contents = array();
 $dir = realpath($dir);
 
 if (is_dir($dir)) {
 $files = scandir($dir);
 
 foreach ($files as $file) {
 if ($file != '.' && $file != '..' && $file != 'events.txt') {
 if (substr($file, -strlen($suffix)) == $suffix) {
 $contents[] = $file;
 }
 }
 }
 
 foreach ($contents as $key => $item) {
 if (strlen($item) != 14) {
 unset($contents[$key]);}
} 

 foreach ($contents as $key => $item) {
 if (substr($item,0,1) != "2") {
 unset($contents[$key]);}
} 

 }

 $contents_json = json_encode($contents);
 file_put_contents($dir . '/events.txt', $contents_json);
 
}
list_contents('archives','.txt');
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM