|
|
|
ถ้าต้องการกำหนดสคริปต์ ให้จำกัดผู้ใช้ให้อัพโหลดได้เฉพาะไฟล์รูปภาพ .gif กับ .jpeg เท่านั้น และขนาดไฟล์ต้องไม่เกิน 20 kb ด้วย |
|
|
|
|
|
|
|
ถ้าต้องการกำหนดสคริปต์
ให้จำกัดผู้ใช้ให้อัพโหลดได้เฉพาะไฟล์รูปภาพ .gif กับ .jpeg เท่านั้น และขนาดไฟล์ต้องไม่เกิน 20 kb ด้วย
จะกำหนดอย่างไรค่ะ
Code (PHP)
<?php
session_start();
$user = $_SESSION["valid_user"];
$pass = $_SESSION["valid_pass"];
include "head.inc.php";
include "css.inc.php";
?>
<link rel="stylesheet" type="text/css" href="js/validationEngine.jquery.css" media="screen" />
<!--เพิ่ม JavaScript สำหรับการทำ Multiple List ระหว่างกลุ่มกับสาขา -->
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<!-- Jquery สำหรับตรวจสอบฟอร์มมีการเพิ่มเข้ามาใหม่ โดย 2 บรรทัดนี้จะต้องอยู่ภายในแท็ก Head เสมอ -->
<script type="text/javascript" src="js/jquery.validationEngine.js"></script>
<link rel="stylesheet" href="js/validationEngine.jquery.css" type="text/css"/>
<style type="text/css">
<!--
.style12 {color: #FF0000}
.style13 {color: #999999}
-->
</style>
<table align="center" width="100%" border="0" class="boder" height="500" cellpadding="0" cellspacing="0">
<tr><td width="20%" valign="top">
<?php
include "left.inc.php";
?>
</td><td>
</td><td width="5%">
</td><td>
</td><td width="75%" valign="top">
<script language="javascript">
function CheckNum(){
if (event.keyCode < 48 || event.keyCode > 57){
event.returnValue = false;
}
}
</script>
<?php
if (isset($_POST["sendadd"])){
process_form_add();
}elseif(isset($_POST["sendedit"])){
process_form_edit();
}elseif($_GET["add"]=="add"){
show_form_add();
}elseif($_GET["edit"]=="edit"){
form_edit();
}elseif($_GET["edit"]=="delete"){
process_form_delete();
}elseif(isset($_POST["submitsearch"])){
show_form_edit();
}else{
show_form_edit();
}
function show_form_add() {
include "connect.inc.php";
$sql = "SELECT MAX(dog_id) as dog_id FROM dog";
$result = mysql_db_query($dbname, $sql);
$row = mysql_fetch_array($result);
$num=$row[dog_id]+"1";
$num1 = "0000000000".$num; // เชื่อม ตัวเลขเข้าด้วยกัน
$num1 =substr($num1,strlen($num1)-3); // ตัดตัวเลขให้เหลือเพียง
echo <<<HTMLBLOCK
<table align="center" width="100%" border="0">
<tr><td class="style4" height="30">
<b>เพิ่มข้อมูลสุนัข</b>
</td></tr>
</table>
<form method="POST" action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" name="frmMain">
<table width="100%" align="center" border="0" class="style6">
<tr><td align="center">
<table width="100%" border="0" class="style3">
<tr><td colspan="2" align="center"><img src="icon/dog_icon.gif" width="200"></td></tr>
<tr><td align="right" width="35%" height="30">รหัส : </td><td>Dog$num1</td></tr>
<tr><td align="right" width="35%">ชื่อสุนัข : </td><td><input type="text" name="name" size="35" class="validate[required]"><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">ชื่อสายพันธุ์ : </td>
<td><select name="species" class="validate[required]" >
<option value="" align="center">-----เลือกสายพันธุ์-----</option>
HTMLBLOCK;
$sql = "SELECT * FROM species order by spe_name asc";
$result = mysql_db_query($dbname, $sql);
while($row = mysql_fetch_array($result)){
echo "<option value='$row[spe_id]'>$row[spe_name]</option>";
}
$d = date('d/m/Y');
echo <<<HTMLBLOCK
</select><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">เพศ : </td><td>
<input type="radio" name="sex" value="ผู้" class="validate[required] radio" checked> ตัวผู้ <input type="radio" name="sex" class="validate[required] radio" value="เมีย"> ตัวเมีย <span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">อายุ : </td><td><input type="text" name="age" size="3" maxlength="2" onKeyPress="CheckNum()" class="validate[required]"> ปี <span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">ความสมบูรณ์ของร่างกาย : </td><td><input type="text" name="integrity" size="50" class="validate[required]"><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">อุปนิสัย : </td><td><input type="text" name="habit" size="50" class="validate[required]"><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">วันที่รับเข้ามูลนิธิ : </td><td><input type="text" name="date" size="10" value="$d" class="validate[required]" ><a href="javascript:displayDatePicker('date')" ><img border="0" src="icon/date.gif" width="25"></a><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">หมายเหตุ : </td><td><input type="text" name="note" size="50"></td></tr>
<tr><td align="right" width="35%">รูป : </td><td><input type="file" name="userfile"></td></tr>
<tr><td colspan="2" align="center"><br><input type='submit' name='sendadd' value=' บันทึก '></td></tr>
</table>
</td></tr>
</table>
<input type="hidden" name="id" value="$num">
</form>
HTMLBLOCK;
}
function process_form_add() {
include "connect.inc.php";
$id = $_POST["id"];
$name = $_POST["name"];
$species = $_POST["species"];
$sex = $_POST["sex"];
$age = $_POST["age"];
$integrity = $_POST["integrity"];
$habit = $_POST["habit"];
$date = $_POST["date"];
$note = $_POST["note"];
$img = "{$_FILES['userfile']['name']}";
if($img!=""){
$sur = strrchr($_FILES['userfile']['name'], "."); //ตัดนามสกุลไฟล์เก็บไว้
$newfilename = (Date("dmy_His10").$sur); //ผมตั้งเป็น วันที่_เวลา.นามสกุล
copy($_FILES["userfile"]["tmp_name"],"dog/".$newfilename); //แล้วค่อยเก็บลงไฟล์
$img=$newfilename;
$sql = "INSERT INTO dog (dog_id, dog_name, spe_id, dog_sex, dog_age, dog_integrity, dog_habit, dog_date, dog_note, dog_img)
VALUES ('$id', '$name', '$species', '$sex', '$age', '$integrity', '$habit', '$date', '$note', '$img')";
$result=mysql_db_query($dbname,$sql);
if ($result){
show_form_edit();
}
}else{
$sql = "INSERT INTO dog (dog_id, dog_name, spe_id, dog_sex, dog_age, dog_integrity, dog_habit, dog_date, dog_note)
VALUES ('$id', '$name', '$species', '$sex', '$age', '$integrity', '$habit', '$date', '$note')";
$result=mysql_db_query($dbname,$sql);
if ($result){
show_form_edit();
}
}
}
function page_navi($page, $limit, $range, $count, $send=NULL, $current_style="navi_on", $other_style="navi_out", $target="_self"){
$output = "";
$total = ceil($count/$limit);
$navi_start = $page-$range;
$navi_end = $page+$range;
$send .= (!empty($send))? "&" : NULL;
if($navi_start <= 0) $navi_start = 1;
if($navi_end >= $total) $navi_end = $total;
if($page>1){
$navi_back = $page-1;
if($page > 2)
$output .= "<a href=\"?" . $send . "page=1\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>«</strong></a> ";
$output .= "<a href=\"?" . $send . "page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>‹</strong></a> ";
}
for($i = $navi_start; $i <= $navi_end; $i++){
if($i == $page)
$output .= " $i ";
else
$output .= "<a href=\"?" . $send . "page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
}
if($page < $total){
$navi_next = $page+1;
$output .= "<a href=\"?" . $send . "page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>›</strong></a> ";
if(($page+1) < $total)
$output .= "<a href=\"?" . $send . "page=" . $total . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>»</strong></a>";
}
if($navi_start>$navi_end)
$output .= "<a href=\"?" . $send . "page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
return $output;
}
function search() {
include "connect.inc.php";
echo <<<HTMLBLOCK
<form action="{$_SERVER['PHP_SELF']}" method="post">
<table align="center" width="80%" border="0" class="style3">
<tr><td align="center">
ค้นหา ชื่อสุนัข
<input type='text' name='text1' size='15'>
<select name="text2">
<option value="" align="center">-----เลือกสายพันธุ์-----</option>
HTMLBLOCK;
$sql = "SELECT * FROM species order by spe_name asc";
$result = mysql_db_query($dbname, $sql);
while($row = mysql_fetch_array($result)){
echo "<option value='$row[spe_id]'>$row[spe_name]</option>";
}
echo <<<HTMLBLOCK
</select>
<input type='submit' name='submitsearch' value=' ค้นหา '>
</td></tr>
</table>
</form>
HTMLBLOCK;
}
function show_form_edit() {
include "connect.inc.php";
if(isset($_POST[submitsearch])){
$text1=$_POST[text1];
$text2=$_POST[text2];
}else{
$text1=$_GET[text1];
$text2=$_GET[text2];
}
if($text1!=""){
$send1 = "text1=$text1";
}
if($text2!=""){
if($text1!="" and $text2!=""){
$send2 = "&text2=$text2";
}else{
$send2 = "text2=$text2";
}
}
$send="$send1$send2";
echo <<<HTMLBLOCK
<table align="center" width="100%" border="0">
<tr><td class="style4" height="30">
<b>ข้อมูลสุนัข</b>
</td><td align="right" class="style3"><br>
<a href={$_SERVER['PHP_SELF']}?add=add><img src="icon/IM_31.jpg" width="90" height="69" title = "เพิ่มข้อมูลสุนัข"/> </a>
</td></tr>
</table>
HTMLBLOCK;
search();
$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 20;
$limit_start = ($page-1)*$limit_end;
if($text1!="" and $text2==""){
$sql = "SELECT * FROM dog where dog_name like '$text1%%' order by dog_id asc";
}elseif($text1=="" and $text2!=""){
$sql = "SELECT * FROM dog where spe_id='$text2%%' order by dog_id asc";
}elseif($text1!="" and $text2!=""){
$sql = "SELECT * FROM dog where dog_name like '$text1%%' and spe_id='$text2%%' order by dog_id asc";
}else{
$sql = "SELECT * FROM dog order by dog_id asc";
}
$query_id = mysql_db_query($dbname, $sql . " LIMIT " . $limit_start. "," . $limit_end);
$count = mysql_num_rows($query_id);
$total = mysql_num_rows(mysql_db_query($dbname, $sql));
if($total > 0){
echo <<<HTMLBLOCK
<form method="POST" action="{$_SERVER['PHP_SELF']}">
<table width="100%" align="center" border="0" class="style6">
<tr><td>
HTMLBLOCK;
echo "<table width='100%' align='center' border='0' class='style3'>";
echo "<tr><td width='20%'>หน้า : " . page_navi($page, $limit_end, 4, $total, $send) . "</td><td align='right' width='10%'>[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] ทั้งหมด " . $total . "</td>";
echo "</table>";
echo <<<HTMLBLOCK
</td></tr>
<tr><td align="center" valign="top" height="400">
<br>
<table width="95%" border="1" class="boder2">
<tr align="center" height="30" class="style2">
<td width='15%' background="icon/sub_h_menu.png">รหัส</td>
<td width='30%' background="icon/sub_h_menu.png">ชื่อ</td>
<td width='40%' background="icon/sub_h_menu.png">สายพันธุ์</td>
<td width='5%' background="icon/sub_h_menu.png"></td>
<td width='5%' background="icon/sub_h_menu.png"></td>
<td width='5%' background="icon/sub_h_menu.png"></td>
</tr>
HTMLBLOCK;
$a=0;
while($a<$count){
$row = mysql_fetch_array($query_id);
if($a%2==0){
$color="#E8E8E8";
}else{
$color="#FFFFFF";
}
$a++;
$dog_id = "0000000000".$row[dog_id]; // เชื่อม ตัวเลขเข้าด้วยกัน
$dog_id =substr($dog_id,strlen($dog_id)-3); // ตัดตัวเลขให้เหลือเพียง
echo "<tr bgcolor='$color' class='style3'>";
echo "<td align='center'>Dog$dog_id</td>";
echo "<td> $row[dog_name]</td>";
$sql1 = "SELECT * FROM species WHERE spe_id='$row[spe_id]'";
$result1 = mysql_db_query($dbname, $sql1);
$row1 = mysql_fetch_array($result1);
echo "<td> $row1[spe_name]</td>";
?>
<td align='center'><a href='dogs-detail.php?id=<?=$row[dog_id]; ?>' onclick="window.open(this.href, 'dogs_detail', 'width=600,height=600,left=350,top=50,scrollbars,resizable'); return false;"><img src='icon/detail.gif' border='0' width='20' alt='ดูรายละเอียด'></a></td>
<?php
echo "<td align='center'><a href='{$_SERVER['PHP_SELF']}?edit=edit&id=$row[dog_id]'><img src='icon/b_edit.png' border='0' width='15' alt='แก้ไขข้อมูล'></a></td>";
echo "<td align='center'><a href='{$_SERVER['PHP_SELF']}?edit=delete&id=$row[dog_id]' onclick=\"return confirm('คุณแน่ใจว่าจะลบข้อมูลนี้ ?')\"><img src='icon/b_drop.png' border='0' width='15' alt='ลบข้อมูล'></a></td>";
echo "</tr>";
}
echo <<<HTMLBLOCK
</table>
</td></tr>
<tr><td>
HTMLBLOCK;
echo "<br><table width='100%' align='center' border='0' class='style3'>";
echo "<tr><td>หน้า : " . page_navi($page, $limit_end, 4, $total, $send) . "</td><td align='right'>[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] ทั้งหมด " . $total . "</td>";
echo "</table>";
echo <<<HTMLBLOCK
</td></tr>
</table>
</form>
HTMLBLOCK;
}else{
echo '<table width="70%" align="center" border="0" height="250">';
echo "<tr><td align='center' height='100'><font color='red'><b>ไม่พบข้อมูล</b></font></td></tr>";
echo "</table><br>";
}
}
function process_form_edit() {
include "connect.inc.php";
$id = $_POST["id"];
$name = $_POST["name"];
$species = $_POST["species"];
$sex = $_POST["sex"];
$age = $_POST["age"];
$integrity = $_POST["integrity"];
$habit = $_POST["habit"];
$date = $_POST["date"];
$note = $_POST["note"];
$img = "{$_FILES['userfile']['name']}";
if($img!=""){
$sur = strrchr($_FILES['userfile']['name'], "."); //ตัดนามสกุลไฟล์เก็บไว้
$newfilename = (Date("dmy_His10").$sur); //ผมตั้งเป็น วันที่_เวลา.นามสกุล
copy($_FILES["userfile"]["tmp_name"],"dog/".$newfilename); //แล้วค่อยเก็บลงไฟล์
$img=$newfilename;
$sql = "update dog set dog_name='{$name}', spe_id='{$species}', dog_sex='{$sex}', dog_age='{$age}', dog_integrity='{$integrity}', dog_habit='{$habit}', dog_date='{$date}', dog_note='{$note}', dog_img='{$img}' where dog_id='{$id}'";
$result=mysql_db_query($dbname,$sql);
if ($result){
show_form_edit();
}
}else{
$sql = "update dog set dog_name='{$name}', spe_id='{$species}', dog_sex='{$sex}', dog_age='{$age}', dog_integrity='{$integrity}', dog_habit='{$habit}', dog_date='{$date}', dog_note='{$note}' where dog_id='{$id}'";
$result=mysql_db_query($dbname,$sql);
if ($result){
show_form_edit();
}
}
}
function form_edit() {
include "connect.inc.php";
$id=$_GET[id];
$sql = "SELECT * FROM dog where dog_id='$id'";
$query = mysql_db_query($dbname, $sql);
$count = mysql_num_rows($query);
$row = mysql_fetch_array($query);
$dog_id = "0000000000".$row[dog_id]; // เชื่อม ตัวเลขเข้าด้วยกัน
$dog_id =substr($dog_id,strlen($dog_id)-3); // ตัดตัวเลขให้เหลือเพียง
echo <<<HTMLBLOCK
<table align="center" width="100%" border="0">
<tr><td class="style4" height="30">
<b>แก้ไขข้อมูลสุนัข</b>
</td></tr>
</table>
<form method="POST" action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" name="frmMain">
<table width="100%" align="center" border="0" class="style6">
<tr><td align="center">
<table width="100%" border="0" class="style3">
HTMLBLOCK;
if($row[dog_img]!=""){
echo "<tr><td colspan='2' align='center'><img src='dog/$row[dog_img]' width='200'></td></tr>";
}else{
echo '<tr><td colspan="2" align="center"><img src="icon/dog_icon.gif" width="200"></td></tr>';
}
echo <<<HTMLBLOCK
<tr><td align="right" width="35%" height="30">รหัส : </td><td>Dog$dog_id</td></tr>
<tr><td align="right" width="35%">ชื่อสุนัข : </td><td><input type="text" name="name" size="35" value="$row[dog_name]" cclass="validate[required]"><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">ชื่อสายพันธุ์ : </td><td><select name="species" >
HTMLBLOCK;
$sql1 = "SELECT * FROM species order by spe_name asc";
$result1 = mysql_db_query($dbname, $sql1);
while($row1 = mysql_fetch_array($result1)){
if($row1[spe_id]==$row[spe_id]){
$selected="selected";
}else{
$selected="";
}
echo "<option value='$row1[spe_id]' $selected>$row1[spe_name]</option>";
}
echo <<<HTMLBLOCK
</select><span class="style12">*</span></td></tr>
HTMLBLOCK;
if($row[dog_sex]=="ผู้"){
echo <<<HTMLBLOCK
<tr><td align="right" width="35%">เพศ : </td><td><input type="radio" name="sex" value="ผู้" checked> ตัวผู้ <input type="radio" name="sex" value="เมีย"> ตัวเมีย<span class="style12">*</span></td></tr>
HTMLBLOCK;
}elseif($row[dog_sex]=="เมีย"){
echo <<<HTMLBLOCK
<tr><td align="right" width="35%">เพศ : </td><td><input type="radio" name="sex" value="ผู้"> ตัวผู้ <input type="radio" name="sex" value="เมีย" checked> ตัวเมีย</td></tr>
HTMLBLOCK;
}else{
echo <<<HTMLBLOCK
<tr><td align="right" width="35%">เพศ : </td><td><input type="radio" name="sex" value="ผู้"> ตัวผู้ <input type="radio" name="sex" value="เมีย"> ตัวเมีย</td></tr>
HTMLBLOCK;
}
echo <<<HTMLBLOCK
<tr><td align="right" width="35%">อายุ : </td><td><input type="text" name="age" size="3" maxlength="2" value="$row[dog_age]" onKeyPress="CheckNum()" class="validate[required]"> ปี<span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">ความสมบูรณ์ของร่างกาย : </td><td><input type="text" name="integrity" size="50" value="$row[dog_integrity]" class="validate[required]"><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">อุปนิสัย : </td><td><input type="text" name="habit" size="50" value="$row[dog_habit]" class="validate[required]"><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">วันที่รับเข้ามูลนิธิ : </td><td><input type="text" name="date" size="10" value="$row[dog_date]" class="validate[required]"><a href="javascript:displayDatePicker('date')"><img border="0" src="icon/date.gif" width="25"></a><span class="style12">*</span></td></tr>
<tr><td align="right" width="35%">หมายเหตุ : </td><td><input type="text" name="note" size="50" value="$row[dog_note]"></td></tr>
<tr><td align="right" width="35%">เปลี่ยนรูป : </td><td><input type="file" name="userfile"></td></tr>
<tr><td colspan="2" align="center"><br><input type='submit' name='sendedit' value=' บันทึก '></td></tr>
</table>
</td></tr>
</table>
<input type="hidden" name="id" value="$row[dog_id]">
</form>
HTMLBLOCK;
}
function process_form_delete() {
include "connect.inc.php";
$id = $_GET["id"];
$sql = "delete from dog where dog_id='$id'";
$result=mysql_db_query($dbname,$sql);
if ($result){
show_form_edit();
}
}
?>
</td></tr>
</table>
<?php
include "footer.inc.php";
?>
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2012-05-03 15:55:55 |
By :
Baitong_ch |
View :
1309 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
$path = "../uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$sql=mysql_query("UPDATE member SET profile_image='$actual_image_name' WHERE mem_id=$_SESSION[id]");
if($sql){
echo ("<meta Http-equiv='refresh' Content='1; Url=../'>");
}
echo "<img src='../uploads/".$actual_image_name."' class='preview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
|
|
|
|
|
Date :
2012-05-03 16:16:12 |
By :
Manussawin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณน่ะค่ะ ^^
|
|
|
|
|
Date :
2012-05-03 16:24:05 |
By :
phonyamon |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีอีกวิธีครับ ตรวจสอบจาก $_FILES["file"]["type"] ก็ได้ครับ
|
|
|
|
|
Date :
2012-05-03 16:43:39 |
By :
13ankster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|