- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <title>apoo</title>
- </head>
- <body>
- <?php
- //如果收到提交請求
- if ($_POST['submitted'] == "yes"){
- $appimage = true;
- //檢查圖片是否是0長度.
- try {
- if ($_FILES['image']['size'] == 0)
- {
- $appimage = false;
- throw new exception ("對不起,不能上傳其它格式圖片.");
- }
- } catch (exception $e)
- {
- echo $e->getmessage();
- } //檢查文件大小.
- try {
- if ($_FILES['image']['size'] > 500000)
- {
- $appimage = false;
- //顯示錯誤信息.
- throw new exception ("對不起,圖片文件不能超過: " . intval ($_FILES['image']['size'] / 1000) . "KB");
- }
- } catch (exception $e)
- {
- echo $e->getmessage();
- }
- // 允許上傳的文件類型
- $allowinput = array ("image/jpeg","image/pjpeg");
- try {
- if (!in_array ($_FILES['image']['type'],$allowinput))
- {
- $appimage = false;
- throw new exception ("錯誤,文件類型必須爲 .jpg文件. 我們上傳的文件類型爲: " . $_FILES['image']['type'] . "");
- }
- } catch (exception $e)
- {
- echo $e->getmessage ();
- }
- //如果通過提交,則移動到目錄中,顯示之.
- if ($appimage){
- try {
- if (!move_uploaded_file ($_FILES['image']['tmp_name'],"uploads/".$_FILES['image']['name'])){
- $goodtogo = false;
- throw new exception ("上傳文件的uploads目錄有錯誤,請檢查屬性.");
- }
- } catch (exception $e)
- {
- echo $e->getmessage ();
- }
- }
- if ($appimage){
- //顯示新上傳的圖片
- ?>
- <img src="uploads/<?php echo $_FILES['image']['name']; ?>" alt="" title="" />
- <?php } ?>
- <br /> <a href="Javascript:history.back(-1)">再次上傳 </a>
- <?php }
- //顯示錶單.
- if ($_POST['submitted'] != "yes"){
- ?>
- <form action="" method="post" enctype="multipart/form-data">
- <input type="hidden" name="submitted" value="yes" />
- 圖片上傳 (只能上傳.jpg文件, 文件大小不能超過500KB): <br />
- <input type="file" name="image" /> <br />
- <input type="submit" value="提交" style="margin-top: 10px;" />
- </form>
- <?php } ?>
- </body>
- </html>