PHP:File_put_content不会创建该文件

时间:2018-04-24 00:16:39

标签: php file-put-contents

这真的很奇怪,这个PHP代码很长时间都运行良好

<?php
header('Content-type: application/json');

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $data = file_get_contents("php://input");
  $arr = explode(' ',trim($data));
  $file =  $arr[0];
  $my_file = "bet_shared/".$file;
  $sharing_bet = strstr($data," ");
  file_put_contents($my_file, $sharing_bet);
}
else
{
    var_export($_SERVER['REQUEST_METHOD']);
}
?>

现在方法file_put_contents($ my_file,$ sharing_bet)没有在bet_shared目录中创建文件。文件夹和目录中的所有权限都设置为777,因此另一个问题对我来说是不可用的!我错过了一些想法吗?

1 个答案:

答案 0 :(得分:0)

最后我整理出来: 该脚本是从Android应用程序调用的,这是我创建连接和POST一些字符串的java代码

 URL url = new URL(URL_STRING);
                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setConnectTimeout(CONNECTION_TIMEOUT);
                conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                //conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.connect();

                OutputStream os = conn.getOutputStream();
                os.write(jsonBet.getBytes("UTF-8"));
                os.close();

我在下面添加了以下代码:

                if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    Log.i("response = " , br.readLine());
                } else {
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                    Log.i("response two = " , br.readLine());

                }

似乎连接失败而没有设置响应代码