簡體   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