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

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效