duanhongyi2964 2019-07-03 17:17 采纳率: 100%
浏览 52

如何使用ajax和php创建和更新文本文件?

When I call my PHP file directly, it makes a text file exactly as I expected. But when I use AJAX, it doesn't make the text file.

I gave pemissions to directory and files: chmod(0777) and setup allow_url_fopen as "ON".
I made sure the AJAX is calling indexing.php, but no text files are created.

What's going wrong?

$('#btn_index').on('click', function(){
  $.ajax({url: "/indexing.php", success: function(result){
    alert("Indexing is finished");
  }});
});

indexing.php

<?php

$fWrite = fopen(__DIR__."/indexOfFiles.txt","w+") or die('Cannot open file:  ');

if ($handle = opendir('./data/pdffiles')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {


            $wrote = fwrite($fWrite, "$entry".PHP_EOL);
        }
    }
}


closedir($handle);
fclose($fWrite);

$fWrite = fopen(__DIR__."/arrays.txt","w+") or die('Cannot open file:  ');
$wrote = fwrite($fWrite, '{"data": []}');

fclose($fWrite);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 luckysheet
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误