先看看代码,很简单就是保存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真是很难用啊

解决方案 »

  1.   

    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);//关闭
     
      

  2.   

    问题是你的这段代码容错性较好,一般不会出错要调试 customError 的话,你总得人为的制造一些错误吧?
      

  3.   

    try不难用啊……
    顺便发个例子给你,GD查jpeg坏图的
    try
    {
    if (!@imagecreatefromjpeg($file)) throw new Exception($error);
    else return true;
    } catch (Exception $e) {
    return false;
    }