|
 |
|
อยากทราบฟังค์ชั่นในการเช็ค ชนิดของไฟล์ก่อนอัพโหลดครับ |
|
 |
|
|
 |
 |
|
เอาไปลองศึกษาดูครับ อาจไม่ใช้สคริปที่ดีที่สุด แต่ทำงานได้ครับ
Code (PHP)
function chkImg($imgName){ #ค่าที่รับ $_FILES['file']['name']
$imgName = explode('.',$imgName);
$i = count($imgName)-1;
if($imgName[$i]=='jpg' || $imgName[$i]=='gif' || $imgName[$i]=='png'){
return true;
} else {
return false;
}
}
ตัวอย่างการใช้งาน
if(!chkImg($_FILES['file']['name'])){ #ถ้าไฟล์ที่แนบมาไม่ใช่รูปภาพ
echo 'กรุณาแนบรูปเท่านั้น';
} else {
#ทำการอัพโหลดรูป
}
|
 |
 |
 |
 |
Date :
2014-07-27 21:18:28 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาไปประยุกต์ใช้ได้ทุกงาน 
|
 |
 |
 |
 |
Date :
2014-07-28 09:21:46 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ป๊าดดดด มันเป็นฟังก์ชั่น PHP ครับ
Code (PHP)
<html>
<head>
<?PHP
function checkImg($imgName){
$imgName = explode('.', $imgName);
$i = count($imgName)-1;
if($imgName[$i] == 'jpg' || $imgName == 'gif' || $imgName == 'png'){
return true;
}else{
return false;
}
}
?>
</head>
<body>
<?php
if(!checkImg($_FILES['item_image']['name'])){
echo "Please attach image only";
}else{
$path = $_FILES['item_image']['tmp_name'];
$name = $_FILES['item_image']['name'];
move_uploaded_file($path,"item_image/$name");
echo "ok";
}
?>
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-07-29 17:15:37 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ผมนี่เบลอจริงๆ เลยครับ ช่วยอธิบายการทำงานของฟังค์ชั่นได้ไหมครับ ผมไม่เข้าใจตรง $i = count($imgName)-1; อ่ะครับ แล้วกรณีที่ผมจะอัพไฟล์ ทีละหลายๆ ไฟล์ ผมควรปรับแต่งยังไงให้เหมาะสมครับ
ไฟล์ upload.php
Code (PHP)
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action="check.php" method="POST" enctype="multipart/form-data">
<input type="file" name="item_image[]"><br/>
<input type="file" name="item_image[]"><br/>
<input type="file" name="item_image[]"><br/>
<input type="file" name="item_image[]"><br/>
<input type="submit" value="upload">
</form>
</body>
</html>
ไฟล์ check.php
Code (PHP)
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<?php
function checkImg($imgName){//a.jpg
$imgName = explode('.', $imgName);//a.jpg = array([0]-->a [1]-->jpg)
//$imgName[0] = a
//$imgName[1] = jpg
$i = count($imgName)-1;
if($imgName[$i] == 'jpg' || $imgName[$i] == 'gif' || $imgName[$i] == 'png'){
return true;
}else{
return false;
}
}
?>
</head>
<body>
<?php
for($i=0; $i<count($_FILES['item_image']['name']); $i++){
if(!checkImg($_FILES['item_image']['name'][$i])){
echo "Please attech image only<br/>";
}else{
$path = $_FILES['item_image']['tmp_name'][$i];
$name = $_FILES['item_image']['name'][$i];
move_uploaded_file($path,"item_image/$name");
echo "ok<br/>";
}
}
?>
</body>
</html>
|
ประวัติการแก้ไข 2014-07-29 19:04:14
 |
 |
 |
 |
Date :
2014-07-29 19:03:18 |
By :
aliens |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code
<?PHP
function checkImg($imgName){
$imgName = explode('.', $imgName); #บรรทัดนี้ใช้แยกสตริง โดยใช้จุด(.) เป็นตัวแยก เพื่อหานามสกุลไฟล์ โดยจะได้ผลลัพธ์ในรูปแบบ Array
$i = count($imgName)-1; #บรรทัดนี้ใช้หาตำแหน่งของนามสกุล ซึ่งตำแหน่งจะไม่แน่นอน เช่น เราอัพโหลดไฟล์ชื่อ test.123456.jpg จะเห็นได้ว่าเมื่อแยกสตริงแล้ว ตำแหน่งนามสกุลไฟล์จะอยู่ที่ Array ที่ 2 และที่ต้อง count()-1 ก็เพราะเมื่อนับจำนวน Array ที่ได้จากการแยกสตริง เราจะได้จำนวนเต็ม แต่ตำแหน่ง Array จะเริ่มต้นด้วย 0 ดังนั้น 3-1 = 2 นามสกุลไฟล์ที่ต้องการจึงอยู่ตำแหน่ง Array ที่ 2(ตามตัวอย่างที่ทำ)
if($imgName[$i] == 'jpg' || $imgName == 'gif' || $imgName == 'png'){ #บรรทัดนี้ ก็นำนามสกุลไฟล์ที่เราหามา มาตรวจสอบว่าเป็นไฟล์รูปจริงหรือไม่ แต่คุณสามารถปรับโค้ดตามสไตล์คุณได้
return true; #ถ้าใช่ไฟล์รูปจริง ก็บอกว่าจริง
}else{
return false; #ถ้าไม่ใช่ไฟล์รูป ก็บอกว่าไม่จริง
}
}
?>
|
 |
 |
 |
 |
Date :
2014-07-29 21:24:47 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|