|
 |
|
ต้องการให้ขึ้น Alert หากอัพโหลดไฟล์ผิด หรือ ขนาดไฟล์เกิน |
|
 |
|
|
 |
 |
|
จากโค้ด หน้า Save ไม่แน่ใจว่ามีตรงไหนผิดหรือป่าว เพราะไม่ขึ้น Alert อะไรเลย แถมไฟล์ pdf ก็ไม่สามารถ save ในโฟลเดอร์ได้
ได้นำโค้ดจาก https://www.thaicreate.com/community/php-check-size-type-before-upload.html มาประยุกต์ใช้
โค้ดหน้า Form
Code (PHP)
<form action="savedata.php" method="post" enctype="multipart/form-data" name="form1" id="form1" onSubmit="JavaScript:return fncSubmit();">
<table cellspacing="0">
<tr>
<td>วันที่ : </td>
<td><input name="date" type="text" size = "10"/>
<a href="javascript:displayDatePicker('date')"><img border="0" src="Calendar-icon.jpg" width="20" height="20"></a></td>
<td> สาขา : </td>
<td width="200"><select name="branch" id="branch">
<option value="U2" > U2 </option>
<option value="U3" > U3 </option>
<option value="U4" > U4 </option>
</select> </td>
<td> User : </td>
<td ><input name="name_user" type="text" size = "10"/></td>
</tr>
<td> เลขที่ Memo : </td>
<td><input name="no_memo" type="text" size = "10" id="no_memo"/></td>
<td></td>
<td></td>
<td > Admin : </td>
<td><input name="name_admin" type="text" size = "10"/></td>
<tr></tr>
</table>
<table>
<tr> Method </tr>
<td><input name="show" type="radio" value="1" onclick="show_table(this.value);" id="add">Add</td>
<td><input name="show" type="radio" value="2" onclick="show_table(this.value);" id="del"> Delete </td>
<td><input name="show" type="radio" value="3" onclick="show_table(this.value);" id="tranfer"> Tranfer</td>
</table>
<table border="0" cellpadding="0" id="opt2" style="display:none">
<tr>
<td>Option</td>
</tr>
</table>
<table border="0" cellpadding="0" id="stk1" style="display:none">
<tr>
<td><label><input name="option11" type="radio" value="stk" id="stk"/>Stock</label></td>
</tr>
</table>
<table border="0" cellpadding="0" id="tranferboi" style="display:none">
<tr>
<td><label><input name="option11" type="radio" value="scrapboi" id="scrapboi" />Tranfer To Scrap BOI</label></td>
</tr>
</table>
<table border="0" cellpadding="0" id="tranfernonboi" style="display:none">
<tr>
<td><label><input name="option11" type="radio" value="scrapnonboi" id="scrapnonboi"/>Tranfer To Scrap NON-BOI</label></td>
</tr>
</table>
<table>
<td>เอกสารแนบไฟล์</td>
<td><input name="filepdf1" type="file" id="filepdf1"/></td>
</table>
<table>
<td><input name="save" type="Submit" value="บันทึกข้อมูล" id="save" /></td>
</table>
</form>
</body>
</html>
โค้ดหน้า Save
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function clickupload()
{
if(document.form1.filepdf1.value.length==0)
alert("Please select file to upload");
return false;
}
alert("Now uploading please wait");
document.form1.save.disabled=true;
return true;
}
function uploadok(pathfile)
{
document.form1.filepdf1.style.display="none";
document.form1.save.style.display="none";
alert("Upload Complete
"+pathfile);
return true;
}
function over_size(size)
{
alert("File not over 800 KB
"+size+" KB");
reset_file("filepdf1");
document.form1.save.disabled=false;
return false;
}
function wrong_type(type)
{
alert("Please upload only file type .pdf !!"+type);
reset_file("filepdf1");
document.form1.save.disabled=false;.
return false;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>บันทึกข้อมูล</title>
</head>
<body>
<?
sleep(3);
$nomemo = $_FILES['filepdf1']['no_memo'];
$file = $_FILES['filepdf1']['name'];
$typefile = $_FILES['filepdf1']['type'];
$sizefile = $_FILES['filepdf1']['size'];
if($sizefile>204800)
{
?>
<script>
window.parent.over_size("<?php echo $sizefile/1024?>");
</script>
<?
$folder="filepdf/"; // กำหนดโฟลเดอร์ที่จะเก็บ
//$tempfile = $folder.$_POST.$date("Y-m-d").".pdf";
}
elseif (($type==".pdf"))
{
$tempfile = $folder.$_POST['no_memo'].".pdf".$type; // เก็บไฟล์ไว้ที่ โฟลเดอร์ filepdf และเปลี่ยนชื่อไฟล์ตามที่ได้รับค่ามาจาก textbox
$tempfile1= $_POST['no_memo'].".pdf".$type; // เก็บชื่อไฟล์ลงฐานข้อมูล
copy($_FILES['filepdf1']['tmp_name'],$tempfile);
?>
<script>
window.parent.uploadok("<? echo $tempfile1?>");
</script>
<?php
}
else
{
?>
<script>
window.parent.wrong_type("<? echo $_FILES["filepdf1"]["type"]?>");
</script>
<?
}
?>
<?
///////////////////////////////////////////////// Connect DB /////////////////////////////////////////////////////////
$host = "localhost";
$user = "root";
$pass = "1234";
$dbname = "find_memo";
$tblname = "find_memo01";
$dbcon = mysql_connect($host,$user,$pass) or die ("Connect Database");
mysql_select_db ($dbname,$dbcon);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$no_memo = $_POST[no_memo];
$branch = $_POST[branch];
$date = $_POST[date];
$name_user = $_POST[name_user];
$name_admin = $_POST[name_admin];
$show = $_POST[show];
$option11 = $_POST[option11];
$filepdf1 = $_POST[filepdf1];
$sql = "insert into $tblname (memo_01,memo_02,memo_03,memo_04,memo_05,memo_06,memo_07,memo_08) values ('$no_memo','$branch','$date','$name_user','$name_admin','$show','$option11','$tempfile1')";
echo "$sql";
$result = mysql_query($sql);
if($result)
{
echo "บันทึกสำเร็จ !";
}
else
{
echo "บันทึกไม่สำเร็จ !";
}
mysql_close($dbcon);
?>
</body>
</html>
Tag : PHP, MySQL, JavaScript

|
|
 |
 |
 |
 |
Date :
2015-03-25 10:42:38 |
By :
kawakwang |
View :
830 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|
|

|
Load balance : Server 03
|