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);//關閉