我有谷歌几个例子,但似乎没有工作,我想做的是:
if(isset($_POST)) {
// json parse
$json = file_get_contents('php://input');
// save the json files for later
// make sure they contain data
if(filesize($json) < 16 && empty(trim(file_get_contents($json))) )
{
$file = 'links-'.rand(10000000,99999999).".txt";
file_put_contents('Links/' . trim($file), $json);
}
}
?>
如果文件大小为空,不要创建文件,但上面的代码仍然(这是我尝试过的最新代码),我在这里错过了一些明显的东西吗?
file_get_contents()函数将内容读入字符串。一旦它进入内存,你就不需要尝试再次读取它。
if (isset($_POST)) {
// populate the json variable
$json = file_get_contents('php://input');
// if the variable is not empty
// save the content for later
if (!empty($json))
{
$file = 'links-'.rand(10000000,99999999).".txt";
file_put_contents('Links/' . trim($file), $json);
}
}
?>
非常简单的解决方案是解码json,你现在可以检查解码的json是否是一个空数组。
$json = file_get_contents('php://input');
$json = json_decode($json);// decode json`
if (empty($json)) {
// json is empty
}