|
 |
|
ช่วยทีค่ะ เรื่องการ ดัก ตอนอัพโหลดรูปภาพ อยากให้อัพได้เฉพาะไฟล์ .jpg,jpeg,pjpeg กับ gif |
|
 |
|
|
 |
 |
|
ถ้าจะดัก ตอนอัพโหลดรูปภาพ ให้อัพได้เฉพาะไฟล์ .jpg,jpeg,pjpeg กับ gif เราจะต้องเพิ่มโค้ดตรงไหนดีคะ โค้ดที่เราทำเป็นแบบด้านล่างอ่ะค่ะ (ขอบคุณล่วงหน้าค่ะ)
Code (PHP)
if(trim($_FILES["fileUpload"]["tmp_name"]) != "")
{
$images = $_FILES["fileUpload"]["tmp_name"];
$images_file = $_FILES["fileUpload"]["type"];
$images_time = date('dmYHis');
srand((double)microtime()*1000000); //ทำการสุ่มชื่อ
$Pic_name=$random_pic = rand(1,9999999);
if( $images_file == "image/gif" )
{
$filename = $Pic_name.".gif";
}
if (($images_file=="image/jpg")||($images_file=="image/jpeg")||($images_file=="image/pjpeg"))
{
$filename = $Pic_name.".jpg";
}
$new_images = "$images_time"."$filename";
$width=500; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
if( $images_file == "image/gif" )
{
$images_orig = ImageCreateFromGIF($images);
}
if (($images_file=="image/jpg")||($images_file=="image/jpeg")||($images_file=="image/pjpeg"))
{
$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);
ImageGIF($images_fin,"../images/products/".$new_images);
ImageJPEG($images_fin,"../images/products/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
//*** Insert Record ***//
$strSQL = "UPDATE product";
$strSQL .=" SET pro_pic = '".$new_images."' WHERE pro_name = '$_POST[pro_name]'";
$objQuery = mysql_query($strSQL);
}
Tag : PHP

|
ประวัติการแก้ไข 2012-05-06 23:06:54 2012-05-06 23:07:29 2012-05-06 23:09:59
|
 |
 |
 |
 |
Date :
2012-05-06 23:06:04 |
By :
Witchloves |
View :
2213 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
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-07 06:44:56 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แบบง่าย ๆ
Code (PHP)
<?
$filetype=$_FILES["fileUpload"]["type"];
if(($filetype!="image/jpg") and ($filetype!="image/jpeg") and ($filetype!="image/pjpeg") and ($filetype!="image/png") and ($filetype!="image/gif"))
{
$strError = " [ <font color=red>JPG file type is wrong (jpg,png,gif only)</font> ]";
}
else
{
// Upload
}
?>
|
 |
 |
 |
 |
Date :
2012-05-07 06:45:31 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่วินมากๆ เลยค่ะ ^^ ทำได้แล้ว
แบ่งปันโค้ดที่ทำเสร็จแล้วค่ะ
Code (PHP)
if(trim($_FILES["fileUpload"]["tmp_name"]) == ""){ //เช็คค่าว่างรูป ถ้าว่างให้เพิ่มข้อมูลลงฐานข้อมูลเลย
/***Insert Product Info to database***//
echo "<script language=\"JavaScript\">";
echo "alert('Insert Complete');";
echo"window.location='product.php';";
echo "</script>";
} else if(trim($_FILES["fileUpload"]["tmp_name"]) != ""){ //เช็คค่าว่างรูป ถ้าไม่ว่างให้ทำ....
$images = $_FILES["fileUpload"]["tmp_name"];
$images_file = $_FILES["fileUpload"]["type"];
$images_time = date('dmYHis');
srand((double)microtime()*1000000); //ทำการสุ่มชื่อ
$Pic_name=$random_pic = rand(1,9999999);
if( $images_file == "image/gif" )
{
$filename = $Pic_name.".gif";
}
if (($images_file=="image/jpg")||($images_file=="image/jpeg")||($images_file=="image/pjpeg"))
{
$filename = $Pic_name.".jpg";
}
$new_images = "$images_time"."$filename";
$width=500; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
if( $images_file == "image/gif" )
{
$images_orig = ImageCreateFromGIF($images);
}
if (($images_file=="image/jpg")||($images_file=="image/jpeg")||($images_file=="image/pjpeg"))
{
$images_orig = ImageCreateFromJPEG($images);
}
//ตรวจสอบไฟล์
$file = $_FILES["fileUpload"]["type"];
if(($file!="image/jpg") and ($file!="image/jpeg") and ($file!="image/pjpeg") and ($file!="image/gif"))
{
echo"<script language='JavaScript'>";
echo"alert('ไฟล์รูปภาพไม่ถูกต้อง (ใช้ได้เฉพาะ jpg,jpeg,pjpeg,gif เท่านั้น)');";
echo"window.location='bf_product_add.php';";
echo"</script>";
}
else if(($file=="image/jpg") or ($file=="image/jpeg") or ($file=="image/pjpeg") or ($file=="image/gif"))
{
// ถ้าไฟล์ถูกต้อง ทำ...
$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);
ImageGIF($images_fin,"../images/products/".$new_images); //path ที่เก็บรูป
ImageJPEG($images_fin,"../images/products/".$new_images); //path ที่เก็บรูป
ImageDestroy($images_orig);
ImageDestroy($images_fin);
/***Insert Product Info to database***//
//*** Insert Record ***//
$strSQL = "UPDATE product";
$strSQL .=" SET pro_pic = '".$new_images."' WHERE pro_name = '$_POST[pro_name]'";
$objQuery = mysql_query($strSQL);
echo "<script language=\"JavaScript\">";
echo "alert('Insert Complete');";
echo"window.location='product.php';";
echo "</script>";
}//เช็คประเภทไฟล์
}//เช็คค่าว่างรูป
|
ประวัติการแก้ไข 2012-05-07 19:10:49 2012-05-07 19:12:13 2012-05-07 19:13:21 2012-05-07 19:14:56
 |
 |
 |
 |
Date :
2012-05-07 19:09:45 |
By :
Witchloves |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดีใจด้วยครับ
|
 |
 |
 |
 |
Date :
2012-05-07 19:21:45 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|