先上個效果圖:
Sample6_1.php 建立Form:
複製程式碼 程式碼如下:
程式碼如下:
顯示上傳狀態與圖片
上傳圖片函數uploadimg:
複製程式碼 if (obj){ obj.nerHTML - process_upload.php 提供檔案上傳功能: } ?>
程式碼如下:
程式碼如下:
theform.submit();
//在showimg
setStatus ("Loading...","showimg");
} //上傳狀態函數
function setStatus (theStatus, theObj){
obj = document.getElementById(theObj);
複製程式碼
程式碼如下:
//提供圖片類型校驗
$allowedtypes = array("image/ jpeg","image/pjpeg","image/png", "image/x-png","image/gif");
//檔案存放目錄
$savefolder = "images";
//如果有檔案上傳就開始工作
if (isset ($_FILES['myfile'])){
//檢查上傳檔案是否符合$allowedtypes類型
if (in_array($_FILES ['myfile']['type'],$allowedtypes)){
if ($_FILES['myfile']['error'] == 0){
$thefile = "$savefolder/". $_FILES['myfile']['name'];
//透過move_uploaded_file上傳檔案
if (!move_uploaded_file($_FILES['myfile']['tmp_name'], $thefile)){
echo "There was an error uploading the file.";
}
else{
?>
BR>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
} }