donglang6656 2018-08-12 10:43
浏览 88
已采纳

PHP脚本没有创建文件

I have the PHP script below, which is intended to create a file from the payload of a POST request to NGINX. The script echos the POST request contents back to the requester, but I find no new file in /www/www/html/files, which is where data should be saved.

Here is the POST request:

http://142.93.124.54/save.php

The body of the request is data (the key) and This is a test! (the value). The reply to the request is

This is a test! | /var/www/html | /var/www/html/files/ | 
   /var/www/html/files/5b70334ddcecb19896.txt

I did chmod 777 files to see if permissions were part of the problem, but that did not change anything.

<?php
//FILE: save.php
$post_data = $_POST['data'];
echo($post_data);
echo(' | ');
echo(__DIR__);
echo(' | ');
if (!empty($post_data)) {
    $dir = '/var/www/html/files/';
    echo($dir);
    echo(' | ');
    $file = uniqid().getmypid();
    $filename = $dir.$file.'.txt';
    $handle = fopen($filename, "w");
    fwrite($handle, $post_data);
    fclose($handle);
    echo $filename ;
}
?>

NOTE: if you are testing this, you need to do a POST request, not GET.

NOTE 2: I changed the directory to /var/ww/html/file/ and confirmed that this should be correct by echoing __DIR__. I also added a few other echo statements, e.g., one for the final filepath to which the data is written. Alas, I find nothing in var/www/html/files/.

  • 写回答

1条回答 默认 最新

  • dongshengheng1013 2018-08-12 11:48
    关注

    The problem is at $dir = '/files';.

    You are pointing to your root directory. Change it to

    $dir = './files';

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题