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

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

    点赞 打赏 评论

相关推荐 更多相似问题