我在使用file_put_contents时遇到了麻烦.我以为我可以在laravel/php中做这样的事情.
$response = new stdClass();
$key = generateKey(); // generates a 32 bit key
$dir = "/mnt/my-usb";
if (file_put_contents($dir . "/" . "key", $key) === true) {
$response->status = "Fail";
$response->message = "Some user facing message that describes the error";
return Response::json($response, 500);
}
Run Code Online (Sandbox Code Playgroud)
所以我想我可以检查状态代码file_put_contents并返回一个很好的错误消息.但是,当我单步执行此代码时,一旦它到达if语句,如果出现故障,它永远不会进入if语句.我不知道为什么.我已经尝试过没有=== true,我得到了相同的结果.我认为file_put_contents会返回某种错误代码,我可以查找并创建面向用户的消息,但它只是自动返回,我无法创建响应以发送回客户端.有什么想法吗?提前致谢.