|
 |
|
ถามเรื่อง upload รูปหน่อยครับ ใช้ foreach ผม upload รูป แล้ว insert ชื่อ รูปลง db แล้วที่นี้ มันมีปัญหา |
|
 |
|
|
 |
 |
|
ผม upload รูป แล้ว insert ชื่อ รูปลง db แล้วที่นี้ มันมีปัญหา สมมุติ เลือก upload 2 รูป แต่พอเช็คใน db มันดันมี ข้อมูล 3 record แต่รูปก็มี 2 รูป ไม่ทราบว่าเป็นเพราะอะไร
Code (PHP)
<?php
foreach($_FILES as $name => $file)
{
//echo 'input = '.$name.', file = '.$file['name'].', tmp_name = '.$file['tmp_name'].'<br>';
$sqlStmt2 = "select max(img_id+1) as maxid, count(*) as recordcount from news_img ";
$result2 = mysql_query($sqlStmt2);
$line2 = mysql_fetch_array($result2, MYSQL_ASSOC);
if($line2["recordcount"] == 0)
{
$maxId2 = 1;
} else{
$maxId2 = $line2["maxid"];
}
$nextId2 = formatStringId($maxId2, 7);
$sql = "INSERT INTO news_img (img_id, news_id) VALUES ('$nextId2', '$nextId')";
$result = mysql_query($sql);
$photo= $file['tmp_name'];
$phot_name= $file['name'];
$phot_size= $file['size'];
$phot_type= $file['type'];
$ext = strtolower(end(explode('.',$photo_name)));
if($ext =="jpg" or $ext == "jpeg" or $ext =="png" or $ext == "gif")
{
$filename = $nextId2.md5(rand() * time()).".".$ext;
@copy($photo,"../../images2/news/normal/$filename");
$sql1 = "UPDATE news_img SET img_normal='$filename' WHERE img_id='$nextId2'";
$result1 = mysql_query($sql1);
if($ext == "jpg" or $ext=="jpeg")
{
$ori_img = @imagecreatefromjpeg($photo);
}else if ($ext =="png"){
$ori_img = @imagecreatefrompng($photo);
}else if ($ext =="gif"){
$ori_img = @imagecreatefromgif($photo);
}
$ori_size = @getimagesize($photo);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];
$Quality = 100;
if($ori_w >= $ori_h)
{
$new_w = 100;
$new_h = @round(($new_w/$ori_w) * $ori_h);
}else{
$new_h = 100;
$new_w = @round (($new_h/$ori_h) * $ori_w);
}
$new_img = @imagecreatetruecolor($new_w,$new_h);
@imagecopyresized($new_img,$ori_img,0,0,0,0,$new_w,$new_h,$ori_w,$ori_h);
if ($ext == "jpg" or $ext == "jpeg")
{
@imagejpeg($new_img,"../../images2/news/thumbnails/$filename",$Quality);
}elseif ($ext == "png"){
@imagepng($new_img,"../../images2/news/thumbnails/$filename",$Quality);
}elseif ($ext == "gif"){
@imagegif($new_img,"../../images2/news/thumbnails/$filename",$Quality);
}
@imagedestroy($ori_img);
@imagedestroy($new_img);
$sql2 = "UPDATE news_img SET img_thumb='$filename' WHERE img_id='$nextId2' ";
$result2 = mysql_query($sql2);
}
}
?>
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-05-21 11:12:12 |
By :
งมเข็ม |
View :
861 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดาเอานะครับ เพราะยังไม่เห็น form คงจะมี <input type="file"> สามอันมั๊งครับ
|
 |
 |
 |
 |
Date :
2009-05-21 11:54:28 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
form มีครับ แต่ code ที่เอามาให้ดู ไม่ได้เอา form มาด้วย เอาเฉพาะส่วนที่ประมวลผลมาให้ดูครับ
|
 |
 |
 |
 |
Date :
2009-05-21 13:23:52 |
By :
งมเข็ม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเป็นไปได้อยากขอดู form ครับเอาไว้เป็นตัวอย่างในวันหลังได้
แต่เห็นคุณเขียน comment อยู่แล้วงั้นเอาแบบนี้ก็ได้ครับ
<?php
foreach($_FILES as $name => $file)
{
//echo 'input = '.$name.', file = '.$file['name'].', tmp_name = '.$file['tmp_name'].'<br>';
if (empty($file['name'])){ continue; } //== ถ้าพบว่าไม่ได้ upload ให้ข้ามไป file ถัดไป ==//
$sqlStmt2 = "select max(img_id+1) as maxid, count(*) as recordcount from news_img ";
$result2 = mysql_query($sqlStmt2);
$line2 = mysql_fetch_array($result2, MYSQL_ASSOC);
if($line2["recordcount"] == 0)
{
$maxId2 = 1;
} else{
$maxId2 = $line2["maxid"];
}
$nextId2 = formatStringId($maxId2, 7);
$sql = "INSERT INTO news_img (img_id, news_id) VALUES ('$nextId2', '$nextId')";
$result = mysql_query($sql);
$photo= $file['tmp_name'];
$phot_name= $file['name'];
$phot_size= $file['size'];
$phot_type= $file['type'];
$ext = strtolower(end(explode('.',$photo_name)));
if($ext =="jpg" or $ext == "jpeg" or $ext =="png" or $ext == "gif")
{
$filename = $nextId2.md5(rand() * time()).".".$ext;
@copy($photo,"../../images2/news/normal/$filename");
$sql1 = "UPDATE news_img SET img_normal='$filename' WHERE img_id='$nextId2'";
$result1 = mysql_query($sql1);
if($ext == "jpg" or $ext=="jpeg")
{
$ori_img = @imagecreatefromjpeg($photo);
}else if ($ext =="png"){
$ori_img = @imagecreatefrompng($photo);
}else if ($ext =="gif"){
$ori_img = @imagecreatefromgif($photo);
}
$ori_size = @getimagesize($photo);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];
$Quality = 100;
if($ori_w >= $ori_h)
{
$new_w = 100;
$new_h = @round(($new_w/$ori_w) * $ori_h);
}else{
$new_h = 100;
$new_w = @round (($new_h/$ori_h) * $ori_w);
}
$new_img = @imagecreatetruecolor($new_w,$new_h);
@imagecopyresized($new_img,$ori_img,0,0,0,0,$new_w,$new_h,$ori_w,$ori_h);
if ($ext == "jpg" or $ext == "jpeg")
{
@imagejpeg($new_img,"../../images2/news/thumbnails/$filename",$Quality);
}elseif ($ext == "png"){
@imagepng($new_img,"../../images2/news/thumbnails/$filename",$Quality);
}elseif ($ext == "gif"){
@imagegif($new_img,"../../images2/news/thumbnails/$filename",$Quality);
}
@imagedestroy($ori_img);
@imagedestroy($new_img);
$sql2 = "UPDATE news_img SET img_thumb='$filename' WHERE img_id='$nextId2' ";
$result2 = mysql_query($sql2);
}
}
?>
|
 |
 |
 |
 |
Date :
2009-05-21 13:40:22 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังเกิน มา 1 Record เหมือนเดิมครับ อันที่เกินมา ไฟล์รูปในโฟเดอร์ไม่มี แต่มีชื่อรูปใน db ซะงั้น จะบ้าตาย
|
 |
 |
 |
 |
Date :
2009-05-21 14:59:43 |
By :
งมเข็ม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีคำสั่ง
$sql = "INSERT INTO news_img (img_id, news_id) VALUES ('$nextId2', '$nextId')";
$result = mysql_query($sql);
บันทึกข้อมูลเข้าไปก่อนที่จะตรวจสอบตัวแปร $ext น่ะครับ
if($ext =="jpg" or $ext == "jpeg" or $ext =="png" or $ext == "gif")
พอบันทึกข้อมูลเข้าตารางเรียบร้อยแต่ปรากฎว่า ext ไม่เข้ากลุ่ม jpg ก็เลยไม่ copy file ไปให้น่ะครับ
|
 |
 |
 |
 |
Date :
2009-05-21 18:46:52 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|