简体   繁体   中英

php list files in directory and save to disk

I want to list the files in "archives/" and write the decoded array to disk (file name = "archives/events.txt") But what I have doesn't create that file.

xhr.send(data); // gives: Uncaught ReferenceError: data is not defined

What am I doing wrong?

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');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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