|
 |
|
สอบถามเรื่อง การกำหนดขนาดไฟล์ภาพ (Files Size) ก่อนอัพโหลด |
|
 |
|
|
 |
 |
|
Code แก้ไขข่าว ของผมเป็นแบบนี้ครับ
Code (PHP)
<?PHP
$id = $_POST['id'];
$n_keyword = $_POST['n_keyword'];
$n_description = $_POST['n_description'];
$n_title = $_POST['n_title'];
$n_h1 = $_POST['n_h1'];
$n_subject = $_POST['n_des'];
$n_detail = $_POST['n_detail'];
$n_page = $_POST['n_page'];
$date = $_POST['date'];
$prd_del = $_POST['prd_del'];
$fileupload=$_FILES['fileupload'] ['tmp_name'];
$fileupload_name=$_FILES['fileupload'] ['name'];
$fileupload_size=$_FILES['fileupload']['size'];
$fileupload_type=$_FILES['fileupload']['type'];
$company = company_;
include("../connect/connect.php");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if ($chkdel=="1") {
$sql3="delete news set pic=''
where id = '$id' ";
$result3=mysql_db_query($dbname,$sql3);
}
if ($fileupload) {
$array_last=explode (".",$fileupload_name);
$c=count ($array_last)-1;
$lastname=strtolower($array_last[$c]);
if ($lastname=="gif" or $lastname=="jpg" or
$lastname=="jpeg") {
$newname = $company .time().".jpg";// time= ชื่อรูป
copy($fileupload,"../upload/".$newname);
$sql3= "UPDATE vmc_news SET `pic` = '$newname'
where id = '$id' ";
//echo"$sql3";
$result3 = mysql_db_query ($dbname,$sql3);
}
@unlink("../upload/$prd_del");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$sql = "update news set
n_keyword = '$n_keyword',
n_description = '$n_description',
n_title = '$n_title',
n_h1 = '$n_h1',
n_subject = '$n_subject',
n_detail = '$n_detail',
n_page = '$n_page',
date = '$date'
where id = '$id'";
$result = mysql_db_query($dbname, $sql);
if($result) {
//echo $sql;
echo"<script>alert('แก้ไขเรียบร้อยแล้ว');window.location='vmc-news.php';</script>";
}else{
echo"<script>alert('ไม่สามารถแก้ไขได้');history.back();<script>";
}
?>
ซึ่งก็แก้ไขได้ตามปรกติ แต่ผมเปลี่ยน Code จาก เป็น
Code (PHP)
if ($fileupload) {
$array_last=explode (".",$fileupload_name);
$c=count ($array_last)-1;
$lastname=strtolower($array_last[$c]);
เป็น
Code (PHP)
if ($fileupload['size']>0) {
if($fileupload['size']>1024*120)//กำหนดให้ไม่เกิน __kbให้คำนวน***1024= 1Kb
{
echo"<script>alert('please upload file not over 120 kb');history.back();</script>>";
exit ();
}
$array_last=explode (".",$fileupload_name);
$c=count ($array_last)-1;
$lastname=strtolower($array_last[$c]);
เพื่อกำหนดขนาดของ file แต่ไม่ error และ ไม่สามารถอัพไฟล์ได้ ครับ รวมทั้ง ต่อให้ใส่รูปที่มีขนาดเกิน 120 kb ก็ไม่มีการ alert ออกมาด้วยครับ ไม่รู้จะแก้ยังไงดี
ขอขอบพระคุณล่วงหน้าครับ
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2012-07-26 11:10:05 |
By :
Black_Hawk |
View :
6504 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูเป็นแนวทางแล้วกันน่ะครับ
Code (PHP)
<form enctype="multipart/form-data" name="frmupload" method="post">
<input type="file" name="myFile">
<input type="submit">
</form>
<?
if(!empty($_FILES['myFile']['name'])){
$size = $_FILES['myFile']['size'];
if($size>(1024*120)){
echo "please select image size not over 120kb.";
}else{
echo "Completed";
}
}
?>
|
 |
 |
 |
 |
Date :
2012-07-26 11:43:48 |
By :
mangkunzo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$images = $_FILES["filUpload"]["tmp_name"][$i];
$images_file = $_FILES["filUpload"]["type"][$i];
$Pic_name=$id;
if (($images_file=="image/jpg")||($images_file=="image/jpeg")||($images_file=="image/pjpeg"))
{
$filename = $Pic_name.".jpg";
}
$new_images = "$filename";
$path= "myfile/".$new_images;
{
if(copy($_FILES['filUpload']['tmp_name'][$i], $path))
{
$width=100;
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"myfile/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
เคย Copy มาใช้ จำไม่ได้ว่าใช้แบบไหน ฮ่า ๆ
|
 |
 |
 |
 |
Date :
2012-07-26 12:15:02 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 2 เขียนโดย : apisitp เมื่อวันที่ 2012-07-26 12:15:02
รายละเอียดของการตอบ ::
Code (PHP)
$images = $_FILES["filUpload"]["tmp_name"][$i];
$images_file = $_FILES["filUpload"]["type"][$i];
$Pic_name=$id;
if (($images_file=="image/jpg")||($images_file=="image/jpeg")||($images_file=="image/pjpeg"))
{
$filename = $Pic_name.".jpg";
}
$new_images = "$filename";
$path= "myfile/".$new_images;
{
if(copy($_FILES['filUpload']['tmp_name'][$i], $path))
{
$width=100;
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"myfile/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
เคย Copy มาใช้ จำไม่ได้ว่าใช้แบบไหน ฮ่า ๆ
ลองทำดูแล้วครับ ได้แบบนี้
Code (PHP)
if(!empty($_FILES['fileupload'])){
$size = $_FILES['fileupload']['size'];
if($size>(1024*500))
{
echo"<script>alert('please upload file not over 500 kb');history.back();</script>>";
exit ();
}
if(trim($_FILES["fileupload"]["tmp_name"]) != "")
{
$images = $_FILES["fileupload"]["tmp_name"];
$new_images = $company .time().".jpg";//
$width=834; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=306;
$images_orig = ImageCreateFromJPEG($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"../upload/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
}
แต่รู้สึกว่าภาพมันจะลดขนาดไฟล์ภาพ (Files Size) มากเกินไปอ่ะครับกลายเป็นภาพมัวๆ ไปเลย แต่ยังไงขอบคุณสำหรับแนวทางนะครับ
ขอบคุณสำหรับคุณ mangkunzo และ คุณ apisitp นะครับ ผมได้ความรู้เพิ่มขึ้นเยอะเลย 
|
 |
 |
 |
 |
Date :
2012-07-30 10:16:00 |
By :
Black_Hawk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|