PHP文件放置内容 - 连接断开时为空打印

时间:2017-10-02 10:37:43

标签: php json

我向服务器发送一些数据,然后从db获取另一个数组。 然后我需要创建一个包含所有数据的 JSON 文件 问题是有时当客户端和服务器之间的连接断开时,记录器在notepad ++中打印空行(或mac中的空行)。 我的问题是如何防止这种情况并在文件放置内容创建空文件时发送错误

也许file_put_contents()是unicode或其他问题? 对于数组中的exmaple,将打印到json我有:

/images/folder/x.jpg

代码:

从我的客户端获取params后(也许他们此时为空) 如何确保php不会创建损坏的文件

$file = $this->getPath();
    $jsonData = json_encode([
        'key'           => time(),
        'key1'           => $paramsFromClientProbablyNull,
        'key2'        =>"constant",
        'key2'   => "constant" ,
        'list'  => $listOfUrls
    ]);

    if ($jsonData === false)
        throw new Exception("Could not encode json");

    //write json 
    if(!file_put_contents($file, $jsonData))
        throw new Exception("Could not write");

    return true;

0 个答案:

没有答案