|
 |
|
ดูโค้ด เชค type ก่อน upload ช่วยหน่อยครับ^------^ |
|
 |
|
|
 |
 |
|
ดูโค้ด เชค type ก่อน upload ช่วยหน่อยครับ ถ้าเราอัพไฟล์ถูก มันก็ฟ้อง alert('Allow File type .png .gif .jpeg .jpg Only')
มันไม่ถูกมันก็ฟ้อง alert('Allow File type .png .gif .jpeg .jpg Only')
Code (PHP)
//Check size & type
//$file = strtolower($_FILES["fileUpload"]["name"]);
$sizefile = $_FILES["fileUpload"]["size"];
$type = $_FILES["fileUpload"]["type"];
if($sizefile>204800)
{
echo"<script>alert('File Over Size Allow 200 KB')</script>";
exit();
}
else if(($type==".jpg")||($type==".jpeg")||($type==".gif")||($type==".png"));
{
//$tempfile = time()."-".$file;
echo"<script>alert('Allow File type .png .gif .jpeg .jpg Only')</script>";
exit();
//PHP Upload and Resize Image
if(trim($_FILES["fileUpload"]["tmp_name"]) != "")
{
$images = $_FILES["fileUpload"]["tmp_name"];
$new_images = "Thumbnails_".time()."_".$_FILES["fileUpload"]["name"];
copy($_FILES["fileUpload"]["tmp_name"],"../uploads/".time()."_".$_FILES["fileUpload"]["name"]);
$width=150; //*** Fix Width & Heigh (Autu caculate) ***//
$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,"../uploads/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
}//if
$image=$_POST[$_FILES["fileUpload"]["name"]];
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2013-03-22 10:24:43 |
By :
nottpoo |
View :
1295 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. เงื่อนไขการ check if..else ค่อนข้างที่จะสับสน
2. บรรทัดที่ 10 ของคุณ มีเครื่องหมายปิด
Code (PHP)
<?
$sizefile = $_FILES["fileUpload"]["size"];
$type = $_FILES["fileUpload"]["type"];
if($sizefile>204800){ // เช็คขนาดไฟล์
echo"<script>alert('File Over Size Allow 200 KB')</script>";
exit();
}else if(($type!=".jpg")||($type!=".jpeg")||($type!=".gif")||($type!=".png")){ //ถ้า type ไม่เท่ากับ
echo"<script>alert('Allow File type .png .gif .jpeg .jpg Only')</script>"; // แสดง alert();
exit();
}else if(trim($_FILES["fileUpload"]["tmp_name"]) != ""){ // upload file
$images = $_FILES["fileUpload"]["tmp_name"];
$new_images = "Thumbnails_".time()."_".$_FILES["fileUpload"]["name"];
copy($_FILES["fileUpload"]["tmp_name"],"../uploads/".time()."_".$_FILES["fileUpload"]["name"]);
$width=150;
$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,"../uploads/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
$image=$_POST[$_FILES["fileUpload"]["name"]];
?>
ลองทดสอบตัวอย่างนี้ดูครับ
|
 |
 |
 |
 |
Date :
2013-03-22 10:33:40 |
By :
Ex-[S]i[L]e[N]t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแล้วครับ อัพโหลด ไม่ได้เลย แถมยังฟ้อง alert('Allow File type .png .gif .jpeg .jpg Only') เหมือนเดิมครับ
Code (PHP)
if(trim($_FILES["fileUpload"]["tmp_name"]) !=""){
if($sizefile>204800){ //Check size & type
echo"<script>alert('File Over Size Allow 200 KB')</script>";
header(localtion."product.php");
}else if(($type =="image/jpg")||($type =="image/jpeg")||($type =="image/gif")||($type =="image/png")){//Check type
echo"<script>alert(' Allow File type .png .gif .jpeg .jpg Only.$type')</script>";
header(localtion."product.php");
}else{
//Resize Image & Upload
$images = $_FILES["fileUpload"]["tmp_name"];
$new_images = "Thumbnails_".time()."_".$_FILES["fileUpload"]["name"];
copy($_FILES["fileUpload"]["tmp_name"],"../uploads/".time()."_".$_FILES["fileUpload"]["name"]);
$width=150;
$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,"../uploads/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
}
$image=$_POST[$_FILES["fileUpload"]["name"]];
|
ประวัติการแก้ไข 2013-03-22 11:59:07
 |
 |
 |
 |
Date :
2013-03-22 10:48:56 |
By :
nottpoo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
บรรทัดที่ 8 อนุญาตประเภทไฟล์ พวก jpg png gif ถ้าไม่ใช่ ให้ alert ใช่มั้ยครับ
แต่เท่าที่ดูจาก code ถ้าเป็นพวกไฟล์ jpg png gif ให้ alert();
|
 |
 |
 |
 |
Date :
2013-03-22 10:52:47 |
By :
Ex-[S]i[L]e[N]t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ Ex-[S]i[L]e[N]t
ลองแบบนี้ ก็ไม่ได่นะ
Code (PHP)
else if(($type !="image/jpg")||($type !="image/jpeg")||($type !="image/gif")||($type !="image/png")){//Check type
$type == "image/jpeg" ให้ทำการอัพโหลดต่อไป ถ้าไม่ใช่ให้หลุดเงื่อนไข
|
ประวัติการแก้ไข 2013-03-22 12:09:21
 |
 |
 |
 |
Date :
2013-03-22 12:02:59 |
By :
nottpoo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เงื่อนไข ที่1. ถ้าไฟล์ มากกว่า 204800 alert();
2. อัพโหลดได้เฉพาะ .png .gif .jpeg .jpg ถ้าไม่ใช่ alert();
3. แต่ถ้าไม่ใช่ upload ได้ตามปกติ
อย่างนี้ใช่ไหมครับตามความต้องการ
|
 |
 |
 |
 |
Date :
2013-03-22 12:19:50 |
By :
Ex-[S]i[L]e[N]t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|