Copy to Clipboard引用的內容:[www.bkjia.com]
$uptypes = array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
代碼:
Copy to Clipboard引用的內容:[www.bkjia.com]
<form enctype="multipart/form-data" name="upImage_form" action="upImage.php" method="post">
<table>
<tr>
<td>圖片上傳:
<input name="upfile" type="file" style="width:200;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17">
<input name="Submit" type="submit" value="上傳" style="width:30;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17"><br><br><br>
<span class="red"><b>Hint:</b></span> Maximum File Size: 500KB File Format: *.jpg *.gif *.png
</td>
</tr>
</table>
</form>
代碼:
Copy to Clipboard引用的內容:[www.bkjia.com]
<?php
if($_POST['Submit']=='上傳'){
$file = $_FILES["upfile"];
$fname = $_FILES["upfile"]["name"];
$fname_array = explode('.',$fname);
$extend = $fname_array[count($fname_array)-1];
$MAX_FILE_SIZE = 512000;
//文件當前位置創建picture文件夾,若要在上一層目錄創建則為"../picture/";
$dest_folder = "picture/";
if($extend!=""){
if(!in_array($file["type"],$uptypes)){
echo "只能上傳圖片文件!";
exit;
}
if($file["size"]>$MAX_FILE_SIZE){
echo "圖片大小不能超過512KB!";
exit;
}
if(!file_exists($dest_folder)){
mkdir($dest_folder);
}
$randval = date('Ymd').rand();
$uploadfile = $dest_folder.$randval.'.'.$extend;
echo 'uploadfile: '.$uploadfile ;
if(move_uploaded_file($_FILES["upfile"]["tmp_name"],$uploadfile)){
echo "圖片上傳成功!";
}else{
echo "圖片上傳失敗!";
}
}
}
?>