PHP捕捉異常的有關問題

來源:互聯網
上載者:User
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);//關閉

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.