|
 |
|
กำหนดชนิดของไฟล์ที่อัพโหลด คือจะมี menu ให้เลือกค่ะ โดย value=1 คือ .doc ถ้า value=2 คือ .pdf แล้วจะให้ |
|
 |
|
|
 |
 |
|
Code (PHP)
<?php
///////////////////////addAddition.php สำหรับกรอกข้อมูล///////////////////////////
<script language="javascript">
function checkform()
{
if(form1.title.value=="" )
{
alert ('ยังไม่ได้กรอกชื่อเนื้อหาเสริม');
form1.title.focus();
return false;
}
else if(form1.des.value=="" )
{
alert ('ยังไม่ได้กรอกคำอธิบาย');
form1.des.focus();
return false;
}
}
</script>
<center><img src="images/manageAdd.jpg" width="150" height="105" /><br /><br />
<img src="images/12852_76754.gif" />
</center>
<form id="form1" name="form1" method="post" action="addAddition2.php" enctype = "multipart/form-data" onsubmit="return checkform();">
<table width="550" border="0" align="center">
<tr>
<td width="40%" bgcolor="#0066CC"><span class="style10">ชื่อเนื้อหาเสริม</span></td>
<td width="60" bgcolor="#66CCFF"><input name="title" type="text" value="" size="50" /></td>
</tr>
<tr>
<td width="40%" bgcolor="#0066CC" ><span class="style10">คำอธิบาย</span></td>
<td bgcolor="#66CCFF" width="60">
<textarea name="des" cols="47" rows="3" id="des"></textarea> </td>
</tr>
<tr>
<td width="40%" bgcolor="#0066CC" ><span class="style10">ประเภท</span></td>
<td bgcolor="#66CCFF" width="60">
<select name="selectType" id="selectType">
<option value="1" selected="selected">.doc</option>
<option value="2">.PDF</option>
</select>
</td>
</tr>
<tr>
<td width="40%" bgcolor="#0066CC" ><span class="style10">เลือกไฟล์เนื้อหาเสริม</span></td>
<td bgcolor="#66CCFF" width="60"><input name="fileUpload" type="file" id="fileUpload" value="" /></td>
</tr>
<tr bgcolor="#000033">
<td colspan="2"><div align="center" class="style10">
<input type="submit" name="Submit" value=" เพิ่ม " />
<input type="reset" name="Reset" value="ยกเลิก" />
</div></td>
</tr>
</table>
<p> </p>
<p>
<label></label>
</p>
</form>
?>
Code (PHP)
<?php
///////////////addAddition2.php สำหรับตรวจสอบความถูกต้อง และ save ลง db///////////////////
ob_start();
include("connect.php"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?
//$title = $_POST["title"];
//$des = $_POST["des"];
//$add_id = "3";
//$date = date("Y/m/d H:i:s", mktime(date("H"), date("i")+0, date("s")+0, date("m")+0 , date("d")+0, date("Y")+0));
$title = $_POST["title"];
$des = $_POST["des"];
$type = $_POST["selectType"];
$file = $HTTP_POST_FILES["fileUpload"]["name"];
$path= "myfile/".$file;
if($type==1)
{
if(strchr($file,".")!=".doc" || strchr($file,".")!=".DOC")
{
echo "<script>alert('ไฟล์ที่ส่งมาไม่ใช่ไฟล์ .doc');history.back();</script>";
exit();
}
else
{
copy($HTTP_POST_FILES["fileUpload"]["tmp_name"], $path);
}
}
else if($type==2)
{
if(strchr($file,".")!=".pdf" || strchr($file,".")!=".PDF")
{
echo "<script>alert('ไฟล์ที่ส่งมาไม่ใช่ไฟล์ .pdf');history.back();</script>";
exit();
}
else
{
copy($HTTP_POST_FILES["fileUpload"]["tmp_name"], $path);
}
}
//copy($HTTP_POST_FILES["fileUpload"]["tmp_name"], $path);
$add = "insert into addition(add_title,add_des,add_type,add_resource) values('$title','$des','$type','$file')";
mysql_query($add) or die("Insert Error");
echo "<script>alert('เพิ่มเนื้อหาเสริมเรียบร้อยแล้ว');history.back();</script>";
//echo $file;
ob_end_flush();
mysql_close();
?>
ผลที่ได้คือ จะเด้งหน้าต่างว่าไฟล์ที่ส่งมาไม่ใช่ .doc หรือไม่ก้อ ไฟล์ที่ส่งมาไม่ใช่ .pdf ตลอดเลยค่ะ ไม่ว่าจะตรงกับ menu ที่เลือกไว้หรือไม่ก้อตาม แล้วก้อจะไม่ save ลง db ด้วยค่ะ แก้ยังไงดีค่ะ ช่วยหน่อยนะคะ T_T
|
 |
 |
 |
 |
Date :
2009-04-30 02:19:37 |
By :
pochiiez |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูละกันนะครับ จะไปนอนละ
Code (PHP)
<?php
///////////////addAddition2.php สำหรับตรวจสอบความถูกต้อง และ save ลง db///////////////////
ob_start();
include("connect.php"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?
//$title = $_POST["title"];
//$des = $_POST["des"];
//$add_id = "3";
//$date = date("Y/m/d H:i:s", mktime(date("H"), date("i")+0, date("s")+0, date("m")+0 , date("d")+0, date("Y")+0));
$title = $_POST['title'];
$des = $_POST['des'];
$type = $_POST['selectType'];
$file = $HTTP_POST_FILES['fileUpload']['name'];
$path= "myfile/".$file;
if($type == "1")
{
if(strchr($file,".")!=="doc" || strchr($file,".")!=="DOC")
{
echo "<script>alert('ไฟล์ที่ส่งมาไม่ใช่ไฟล์ .doc');history.back();</script>";
exit();
}
else
{
copy($HTTP_POST_FILES["fileUpload"]["tmp_name"], $path);
}
}
else if($type == "2")
{
if(strchr($file,".")!=="pdf" || strchr($file,".")!=="PDF")
{
echo "<script>alert('ไฟล์ที่ส่งมาไม่ใช่ไฟล์ .pdf');history.back();</script>";
exit();
}
else
{
copy($HTTP_POST_FILES["fileUpload"]["tmp_name"], $path);
}
}
//copy($HTTP_POST_FILES["fileUpload"]["tmp_name"], $path);
$add = "insert into addition(add_title,add_des,add_type,add_resource) values('$title','$des','$type','$file')";
mysql_query($add) or die("Insert Error");
echo "<script>alert('เพิ่มเนื้อหาเสริมเรียบร้อยแล้ว');history.back();</script>";
//echo $file;
ob_end_flush();
mysql_close();
?>
|
 |
 |
 |
 |
Date :
2009-04-30 04:25:08 |
By :
mosaddzero |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|