欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP捕捉异常的有关问题

程序员文章站 2022-06-17 20:26:15
...
PHP捕捉错误的问题
先看看代码,很简单就是保存FLASH提交的betys图片数据。


function customError() { }
set_error_handler("customError");

$destination_folder="face/"; //上传文件路径
$xmlstr =$GLOBALS[HTTP_RAW_POST_DATA];//获取post提交的数据
if(empty($xmlstr))
{
$xmlstr = file_get_contents('php://input');//读取post提交数据的另一种方法
}
if(!file_exists($destination_folder))
{
mkdir($destination_folder);//创建存放图片的目录
}
$jpg = $xmlstr;//得到post过来的二进制原始数据
$imgUrl=$destination_folder.time().rand(0, 99).".png";
$file = fopen($imgUrl,"w");//打开文件准备写入
fwrite($file,$jpg);//写入
fclose($file);//关闭

echo 1;


现在无论如何,都会echo 1,
但是如果我想在保存失败的时候返回-1,比如磁盘满、路径错误或者没有定权限等,
要返回错误给flash来告之用户,这个怎么做呢?
php里面的try真是很难用啊
------解决方案--------------------
function customError() { }
set_error_handler("customError");

$destination_folder="face/"; //上传文件路径
$xmlstr =$GLOBALS[HTTP_RAW_POST_DATA];//获取post提交的数据
if(empty($xmlstr))
{
$xmlstr = file_get_contents('php://input');//读取post提交数据的另一种方法
}
if(!file_exists($destination_folder))
{
mkdir($destination_folder);//创建存放图片的目录
}
$jpg = $xmlstr;//得到post过来的二进制原始数据
$imgUrl=$destination_folder.time().rand(0, 99).".png";
$file = fopen($imgUrl,"w");//打开文件准备写入
if(fwrite($file,$jpg))
{
echo '1'; //写入成功
}
else
{
echo '-1';//写入失败
}
fclose($file);//关闭

PHP捕捉异常的有关问题

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频