|
 |
|
สอบถามเรื่องการเปลี่ยนชื่อไฟล์ก่อนอัพโหลด กรณีที่ชื่อไฟล์ที่อัพโหลด มี . จะเปลี่ยนอย่างไรครับ |
|
 |
|
|
 |
 |
|
เป็นไปได้ไหม ถ้าเราจะเขียน แบบว่า
Code (PHP)
$Str_file = explode(".",$_FILES['file']['name']); // แยกชื่อไฟล์ด้วย "." ค่าที่ได้จะเป็น Array ครับ
// เช่น 12346.gif ค่าที่ได้คือ 0 [123456] และ 1 [gif]
$type = sizeof($str_file)-1;
$new_name="ทดสอบ_upload.".$str_file['$type']; <<<<<<<<
มันจะมีอะไรผิดพลาดรึเปล่าครับ
|
ประวัติการแก้ไข 2014-05-30 11:57:05 2014-05-30 11:58:00
 |
 |
 |
 |
Date :
2014-05-30 11:52:58 |
By :
angelkiller9 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยุ่งยากครับ คิดง่ายๆ ไม่ต้องเปลี่ยน แต่เอาชื่อมาแรนดอมใส่เลย
$rand = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789'),0,15);
$new_name=.$rand."_".$_FILES['file']['name'];
|
 |
 |
 |
 |
Date :
2014-05-30 13:07:25 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมใช้อย่างนี้อยู่ ลองเอาไปปรับเอาดูครับ เผื่อจะมีประโยชน์
Code (PHP)
function chkImg($imgName){
$imgName = explode('.',$imgName);
$i = count($imgName)-1;
if($imgName[$i]=='jpg' || $imgName[$i]=='gif' || $imgName[$i]=='png'){
return true;
} else {
return false;
}
}
#----------ฟังก์ชั่นเปลี่ยนชื่อไฟล์ที่อัพโหลด---------#
function getNewName($imgName){
$imgName = explode('.',$imgName);
$i = count($imgName)-1;
$name = $imgName[$i];
$rand = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-_+'),0,9);
$result = md5($rand.time()).'.'.$name;
return $result;
}
//การใช้งาน
<?PHP
if(chkImg($_FILES['file']['name'])){ //ถ้าไฟล์ที่อัพโหลดมาเป็นไฟล์รูปจริงๆ ก็จะอนุญาตให้อัพโหลด
$newName = getNewName($_FILES['file']['name']);
copy($_FILES['file']['tmp_name'],'files/'.$newName);
} else {
......ไม่อนุญาต.......
}
?>
|
ประวัติการแก้ไข 2014-05-30 13:56:18
 |
 |
 |
 |
Date :
2014-05-30 13:52:04 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|