โดย imakiracon » 14/02/2011 3:50 pm
ผมได้แนบไฟล์ที่ ทำมาให้ดูด้วยครับ ช่วยแนะนำหน่อย
โค้ด: เลือกทั้งหมด
<?php
session_start();
include("../config.inc.php");
include("../function.inc.php");
include_once("loginCheck.php");
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
$categ_id = $_POST['categ_id'];
$product_name = $_POST['product_name'];
$product_price = $_POST['product_price'];
$product_detail = $_POST['product_detail'];
$product_detail1 = $_POST['product_detail1'];
$code = $_POST['code'];
$size = $_POST['size'];
$color = $_POST['color'];
$min_order = $_POST['min_order'];
$note = $_POST['note'];
$sc_id = $_POST['sc_id'];
$status = $_POST['status'];
$come = $_POST['come'];
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
$product_date = date(Ymd);
$prefix = "small_"; // the prefix to be added to the original name
$maxfile = "3000000";//500K
if($userfile_name!=""){
/********* การ Upload รูปและ Resize *********/
if (isset($_FILES['image']['name']))
{
//ตรวจสอบขนาดและชนิด
if ($userfile_type != "image/jpg" && $userfile_type != "image/pjpeg" && $userfile_type != "image/jpeg"){
$msg = "ต้องเป็นนามสกุล .jpg เท่านั้น";
}else if ($userfile_size >= $maxfile){
$msg = "ต้องมีขนาดไม่เกิน ". byte_format($maxfile);
}
if($msg!=""){
?>
<script language="JavaScript">
alert("<?php echo $msg; ?>");
history.go(-1);
</script>
<?php
exit();
}
//เช็คชื่อรูปว่าซ้ำมั้ย
$sql1 = "SELECT p_img_large
FROM products
WHERE p_img_large = '$userfile_name' ";
$result1 = mysql_db_query($dbname, $sql1);
$totalrow = mysql_num_rows($result1);
if($totalrow>=1){
?>
<script language="JavaScript">
alert("Please change picture namne");
window.location.href='index.php?view=addProductCateg&categ_id=<?php echo $categ_id; ?>';
//alert("C");
</script>
<?
}else{
$size_w = 150;
$size_h = 150; // the thumbnail height
$filedirProduct = "../../product/"; // the directory for the original image
$thumbdirProduct = "../images/product/";
if (isset($_FILES['image']['name']))
{
// เริ่มการ Upload รูปและ Resize
$prod_img = $filedirProduct.$userfile_name;
$prod_img_thumb = $thumbdirProduct.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
$sizes = getimagesize($prod_img);
if($sizes[1]>$sizes[0]){
$aspect_ratio = $sizes[1]/$sizes[0];
//$aspect_ratio1 = $sizes[0]/$sizes[1];
if ($sizes[1] <= $size_h){
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_height = $size_h;
$new_width = abs($new_height/$aspect_ratio);
}
}else{
$aspect_ratio = $sizes[1]/$sizes[0];
if ($sizes[0] <= $size_w){
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_width = $size_w;
$new_height = abs($new_width/$aspect_ratio);
}
}
$destimg=imagecreatetruecolor($new_width,$new_height) or die('Problem In Creating image');
$srcimg=imagecreatefromjpeg($prod_img) or die('Problem In opening Source Image');
imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $sizes[0], $sizes[1]) or die('Problem In resampling');
imagejpeg($destimg,$prod_img_thumb,150) or die('Problem In saving');
imagedestroy($destimg);
$sql = "INSERT INTO products(p_code,p_name,c_id,sc_id,p_size,p_color, p_price,p_note, p_img_small,p_img_large, p_full_detail,p_status)
VALUES('$code','$product_name', '$categ_id','$sc_id','$size','$color','$product_price','$note','$prefix$userfile_name', '$userfile_name', '$product_detail1','$status') ";
mysql_db_query($dbname, $sql);
}
}
}
}else{
$sql = "INSERT INTO products(p_code,p_name,c_id,sc_id,p_size,p_color, p_price,p_note, p_full_detail,p_status)
VALUES('$code','$product_name', '$categ_id','$sc_id','$size','$color','$product_price','$note', '$product_detail1','$status') ";
mysql_db_query($dbname, $sql);
}
?>
<script language="JavaScript">
window.location.href='index.php?view=productList&categ_id=<?php echo $categ_id; ?>';
//alert("C");
</script>
[/color]
แนะนำด้วยครับ ผมมือใหม่เรื่องพวกนี้จริงๆ
ผมได้แนบไฟล์ที่ ทำมาให้ดูด้วยครับ ช่วยแนะนำหน่อย
[color=#0000FF]
[code=php]<?php
session_start();
include("../config.inc.php");
include("../function.inc.php");
include_once("loginCheck.php");
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
$categ_id = $_POST['categ_id'];
$product_name = $_POST['product_name'];
$product_price = $_POST['product_price'];
$product_detail = $_POST['product_detail'];
$product_detail1 = $_POST['product_detail1'];
$code = $_POST['code'];
$size = $_POST['size'];
$color = $_POST['color'];
$min_order = $_POST['min_order'];
$note = $_POST['note'];
$sc_id = $_POST['sc_id'];
$status = $_POST['status'];
$come = $_POST['come'];
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
$product_date = date(Ymd);
$prefix = "small_"; // the prefix to be added to the original name
$maxfile = "3000000";//500K
if($userfile_name!=""){
/********* การ Upload รูปและ Resize *********/
if (isset($_FILES['image']['name']))
{
//ตรวจสอบขนาดและชนิด
if ($userfile_type != "image/jpg" && $userfile_type != "image/pjpeg" && $userfile_type != "image/jpeg"){
$msg = "ต้องเป็นนามสกุล .jpg เท่านั้น";
}else if ($userfile_size >= $maxfile){
$msg = "ต้องมีขนาดไม่เกิน ". byte_format($maxfile);
}
if($msg!=""){
?>
<script language="JavaScript">
alert("<?php echo $msg; ?>");
history.go(-1);
</script>
<?php
exit();
}
//เช็คชื่อรูปว่าซ้ำมั้ย
$sql1 = "SELECT p_img_large
FROM products
WHERE p_img_large = '$userfile_name' ";
$result1 = mysql_db_query($dbname, $sql1);
$totalrow = mysql_num_rows($result1);
if($totalrow>=1){
?>
<script language="JavaScript">
alert("Please change picture namne");
window.location.href='index.php?view=addProductCateg&categ_id=<?php echo $categ_id; ?>';
//alert("C");
</script>
<?
}else{
$size_w = 150;
$size_h = 150; // the thumbnail height
$filedirProduct = "../../product/"; // the directory for the original image
$thumbdirProduct = "../images/product/";
if (isset($_FILES['image']['name']))
{
// เริ่มการ Upload รูปและ Resize
$prod_img = $filedirProduct.$userfile_name;
$prod_img_thumb = $thumbdirProduct.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
$sizes = getimagesize($prod_img);
if($sizes[1]>$sizes[0]){
$aspect_ratio = $sizes[1]/$sizes[0];
//$aspect_ratio1 = $sizes[0]/$sizes[1];
if ($sizes[1] <= $size_h){
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_height = $size_h;
$new_width = abs($new_height/$aspect_ratio);
}
}else{
$aspect_ratio = $sizes[1]/$sizes[0];
if ($sizes[0] <= $size_w){
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_width = $size_w;
$new_height = abs($new_width/$aspect_ratio);
}
}
$destimg=imagecreatetruecolor($new_width,$new_height) or die('Problem In Creating image');
$srcimg=imagecreatefromjpeg($prod_img) or die('Problem In opening Source Image');
imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $sizes[0], $sizes[1]) or die('Problem In resampling');
imagejpeg($destimg,$prod_img_thumb,150) or die('Problem In saving');
imagedestroy($destimg);
$sql = "INSERT INTO products(p_code,p_name,c_id,sc_id,p_size,p_color, p_price,p_note, p_img_small,p_img_large, p_full_detail,p_status)
VALUES('$code','$product_name', '$categ_id','$sc_id','$size','$color','$product_price','$note','$prefix$userfile_name', '$userfile_name', '$product_detail1','$status') ";
mysql_db_query($dbname, $sql);
}
}
}
}else{
$sql = "INSERT INTO products(p_code,p_name,c_id,sc_id,p_size,p_color, p_price,p_note, p_full_detail,p_status)
VALUES('$code','$product_name', '$categ_id','$sc_id','$size','$color','$product_price','$note', '$product_detail1','$status') ";
mysql_db_query($dbname, $sql);
}
?>
<script language="JavaScript">
window.location.href='index.php?view=productList&categ_id=<?php echo $categ_id; ?>';
//alert("C");
</script>[/code][/color]
แนะนำด้วยครับ ผมมือใหม่เรื่องพวกนี้จริงๆ