|
 |
|
[php] อยากขอช่วยอธิบายวิธีการอัพไฟล์ใหม่อัพไฟล์เก่าให้ทีครับ |
|
 |
|
|
 |
 |
|
มาอีกแล้วครับ ( วันละกระทู้เลย ตอนนี้ T^T )
สืบเนื่องจากกระทู้นี้ https://www.thaicreate.com/php/forum/057222.html ได้พูดถึงวิธีแก้ที่ผมต้องการพอดี แต่ปัญหาคือ อ่านแล้วไม่ได้ช่วยให้บรรลุวิธีแก้เลยครับ = =
เลยอยากขอให้ช่วยสอนหน่อยว่ามันต้องทำยังไง เขียนโค้ดยังไงบ้างครับ? ( ผมพอเข้าใจว่าให้ดึงค่าชื่อไฟล์มาก่อน แล้วลบไฟล์ จากนั้นค่อยเอาชื่อไฟล์เก่ามาใช้กับไฟล์ใหม่ )
โค้ดส่วนอัพโหลดข้อมูลพร้อมไฟล์ครับ
Code (ASP)
if(is_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'])){
mysql_query("INSERT INTO form_bill( id, recorder, bill_type, storecode, usercode, read_date, bill_month, read1, read2, sum, vat7, sum_all ) values ('','$record','$type','$storecode','$usercode','$date','$billmonth','$read1','$read2','$sum','$vat','$sumall')");
$bill_id = mysql_insert_id();
$pic_name = $day.$month.$year."-".$storecode.".jpg";
$pic_link = "http://choicecm.org/pic/billpic/".$pic_name;
mysql_query("UPDATE form_bill SET pic = '$pic_name' WHERE id = '$bill_id' ");
move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'],"./pic/billpic/".$pic_name);
echo '<img src="pic/choice7-11 small logo.png" align="middle" width="300" height="150" /><br /><br />บันทึกข้อมูลเรียบร้อย';
}
แบบนี้ผมต้องแทรงไอ้โค้ดอัพไฟล์ทับตรงไหน ยังไงครับ ช่วยสั่งสอนด้วยฮะ ( งวดนี้ผมไม่รู้แนวเลย กระทู้ก่อนๆ พอเขียนโค้ดมั่วๆเป็นตัวอย่างให้ได้ ขอโทษทีครับ )
Tag : PHP, MySQL, Windows

|
ประวัติการแก้ไข 2014-08-20 15:12:54 2014-08-20 15:15:12
|
 |
 |
 |
 |
Date :
2014-08-20 15:12:24 |
By :
nimirue |
View :
2394 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ของผม ก็ ทำก่ารเช็ค ว่า มีไฟล์ เก่า อยู่ ใน นั้น ไหม ถ้า มี ให้ลบ อันเก่า ทิ้ง แล้ว นำไฟล์ใหม่ไปใส่ ถ้าไม่มี ก็ ให้ ใส่ได้เลย ครับ
Code (PHP)
$sql2 = "select doc_file from document where doc_id ='$id' ";
$precon2 = mysql_query($sql2);
$check=mysql_fetch_array($precon2);
if ($_FILES['fileupload']['size'] == 0)
{
$namefile = $check['doc_file']; }
else
{
$namefile = $check['doc_file'];
if ($check['doc_file']!='')
{ unlink("fileupload/".$namefile);}
$Str_file = explode(".",$_FILES['fileupload']['name']);
if ($Str_file['0']=='')
{
$namefile='';}
else
{$namedate='Doc_'.date("dmY").'_'.date("H.i A");
$namefile=$namedate.'.'.$Str_file['1'];}
move_uploaded_file($_FILES["fileupload"]["tmp_name"],"fileupload/".$namefile);
}
แล้วเอา $namrfile ไป ใส่ sql ได้เลยครับ กรณี นี้ ผมจะเปลี่ยนชื่อไฟล์ เป็น ในแบบ ของผมเองนะครับ ลองเอาไปดูก่อน (เวลาโชวเทเบิลก็เอาเป็นลิงค์กดโชวไฟล์ได้เลย)
ผมลองเอาโคดบางส่วนมาให้ดูครับ ลองเอาไปดูน่าจะต่อยอดได้บ้างครับ
|
ประวัติการแก้ไข 2014-08-20 16:44:31
 |
 |
 |
 |
Date :
2014-08-20 16:30:07 |
By :
summawat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอามาเผยแพร่อีกอันครับ เป็นการเขียนใส่ฟังก์ชั่นเข้าไปเลย ใช้งานได้ ยกไปอยู่ตรงไหนของเว็บก็ได้ สามารถใช้ร่วมกันได้ทันทีครับ
พร้อมระบบเปลี่ยนชื่อไฟล์กรณีการอัพไฟล์แล้วชื่อซ้ำกัน
ลบไฟล์เก่าทันทีที่เข้าฟังก์ชั่นนี้ ไม่ต้องไปคิดมากให้ปวดหัว ดัดแปลงนิดเดียวใช้ได้ตลอดชีวิตครับ
ระบบไม่ได้รับความซับซ้อน เช่น การขยาย / ย่อรูป ใช้สำหรับอัพไฟล์จริงๆ
ไฟล์ไม่หนักครับ ผมลองถือดูแล้ว
ตัวอย่างฟังก์ชั่น
Code (PHP)
function Uploadimage($oldpic,$FileUpload){
$path = './uploadfile';
$rand = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ23456789'),0,15);
if(trim($_FILES["FileUpload"]["tmp_name"]) != ""){
@unlink($path."/".$oldpic);
$images = $_FILES["FileUpload"]["name"];
$tmp = $_FILES["FileUpload"]["tmp_name"];
$new_images = $rand.'_'.$_FILES["FileUpload"]["name"];
move_uploaded_file($tmp,$path."/".$new_images);
}else{
$images = $oldpic;
$new_images = $images;
}
return $new_images;
}
ตัวอย่าง htmlCode (PHP)
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="oldpic" value="<?=$oldpic?>" /> ///ใส่ข้อมูลรูปเก่าหน้าแก้ไข ตอนกำลังส่งไฟล์ พร้อมตั้งชื่อว่า oldpic
<input type="file" name="FileUpload" /> /// ผมตั้งชื่อไว้ว่า FileUpload
ตัวอย่างหน้าอัพโหลด
Code (PHP)
$FileUpload = $_POST['FileUpload']; /// รับค่าชื่อรูปใหม่เข้ามา
$oldpic = $_POST['oldpic']; /// รับค่าชื่อรูปเก่าเข้ามา
$newimages = Uploadimage($oldpic,$FileUpload); //// ใส่รูปเก่าและรูปใหม่ไว้ในตัวแปร
mysql_query("UPDATE Table SET image='".$newimages."' WHERE id='$id'");
|
ประวัติการแก้ไข 2014-08-20 17:42:04
 |
 |
 |
 |
Date :
2014-08-20 17:40:19 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กลับมาละครับ
ไปลองมาแล้วครับ ทำได้ละครับ ตอนนี้ลบรูปแล้วอัพไฟล์ใหม่ทับได้แล้วครับ แต่ก็แอบเจอปัญหาจุกจิกตามมาอยู่
อันนี้เป็นรูปตอนอัพโหลดลองโค้ดเล่นๆครับ ( จำชื่อไฟล์ ไว้นะครับ )

กดมาแสดงรูปนี้ครับ

ปัญหาคือ ผมอัพรูปใหม่ทับได้สำเร็จครับ ตามภาพเลย

( ชื่อเหมือนไฟล์รูปบนเป๊ะ )
แต่พอไปกดลิ้งรูปแล้ว มันกลับยังโชว์รูปเก่าให้ผมเห็นอยู่ ทั้งๆที่รูปมันไม่มี + วางทับไปแล้ว = =''
งงเลยครับ ขอไปหาสาเหตุแปบ
** อันนี้ code ที่ผมตั้งเงื่อนไขแก้ไขข้อมูลภาพ หลังจากดู 2 - 3 โพสข้างบนประกอบครับ **
Code (ASP)
if ( $HTTP_POST_FILES['pic']['tmp_name'] == null ){
echo '<img src="pic/choice7-11 small logo.png" align="middle" width="300" height="150" /><br /><br />แก้ไขข้อมูลค่าไฟ ไอดี'.$id.'เรียบร้อยแล้ว';
}
else{
$picname = $result['pic'];
unlink("pic/billpic/".$picname);
move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'],"./pic/billpic/".$picname);
echo '<img src="pic/choice7-11 small logo.png" align="middle" width="300" height="150" /><br /><br />แก้ไขข้อมูลค่าไฟ ไอดี'.$id.'เรียบร้อยแล้ว';
}
}
|
 |
 |
 |
 |
Date :
2014-08-22 11:24:57 |
By :
nimirue |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันน่าจะเป็น cache ของระบบครับ ถ้าไม่กระทบอะไร เปลี่ยนชื่อไฟล์ใหม่ดีกว่าครับ  
|
 |
 |
 |
 |
Date :
2014-08-22 14:10:13 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดูโค๊ดตอนโชวไฟล์ครับ
|
 |
 |
 |
 |
Date :
2014-08-22 14:16:44 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|