ต้องการที่จะลบรูปทีละภาพได้อ่าคะ
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ต้องการที่จะลบรูปทีละภาพได้อ่าคะ
ที่สร้างคือ Code PHP คะ ตอนแรกจะทำการอัพโหลดภาพก่อน แล้ว แสดงภาพออกมาให้เห็นนะค่ะ จากนั้นต้องการมีให้ลบรูป
ซึ่งพอกดลบรูปกลายเป็นว่า ลบหมดทุกไฟล์เลยอะคะ จริงๆแล้ว อยากให้กดลบรูปไหน ก็ลบรูปนั้นแค่ภาพเดียว
ช่วยดู Code ทีจร้า ว่าผิดตรงไหน ดูหลายรอบแล้ว ก็ยังแก้ไม่ได้เลย
upload.php
1 | <span class = "Apple-style-span" style= "font-family: 'MS Sans Serif', Tahoma, Thonburi, Bangkok; font-size: x-small; color: rgb(70, 85, 132); line-height: 16px; " ><?php // JQuery File Upload Plugin v1.4.1 by RonnieSan - (C)2009 Ronnie Garcia if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/'; $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; // Uncomment the following line if you want to make the directory if it doesn't exist // mkdir(str_replace('//','/',$targetPath), 0755, true); move_uploaded_file($tempFile,$targetFile); } echo '1'; ?> <span class="Apple-style-span" style="color: rgb(0, 0, 0); white-space: normal; font-size: small; "><b>Deletefiles.php <br type="_moz"></b></span><span class="Apple-style-span" style="white-space: normal; "><?php<br><br>$dir = "files";<br><br>if (is_dir($dir)) {<br>if ($opendir = opendir($dir)) {<br>while (($file = readdir($opendir)) !== false) {<br><br>unlink($dir . "/" . $file);<br>}<br>closedir($opendir);<br>}<br>}<br>?> <br type="_moz"></span><span class="Apple-style-span" style="color: rgb(0, 0, 0); white-space: normal; font-size: small; "><b>ซึ่งพอกดลบรูปแล้ว ขึ้น Error ว่า</b><br><br><span style="color: red; ">Warning: unlink(files/.) [function.unlink]: Permission denied in C:xampplitehtdocsPhotoUploadDeleteFiles.php on line 9<br><br>Warning: unlink(files/..) [function.unlink]: Permission denied in C:xampplitehtdocsPhotoUploadDeleteFiles.php on line 9</span><br><br>รบกวนด้วยนะคะ ขอบคุณล่วงหน้ามากๆๆคะ</span><br type="_moz"></span> |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ง่ะ ขอโทดด้วยคะ ไหงข้อความมันรวมไปหมดเลยอ่า แก้ให้ใหม่นะคะ T_T
upload.php
1 | <span class = "Apple-style-span" style= "font-family: 'MS Sans Serif', Tahoma, Thonburi, Bangkok; font-size: small; line-height: 16px; " ><b><span class = "Apple-style-span" style= "font-weight: normal; font-size: x-small; color: rgb(70, 85, 132); " ><?php // JQuery File Upload Plugin v1.4.1 by RonnieSan - (C)2009 Ronnie Garcia if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/'; $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; // Uncomment the following line if you want to make the directory if it doesn't exist // mkdir(str_replace('//','/',$targetPath), 0755, true); move_uploaded_file($tempFile,$targetFile); } echo '1'; ?></span></b></span> |
1 | <span class = "Apple-style-span" style= "font-family: 'MS Sans Serif', Tahoma, Thonburi, Bangkok; font-size: x-small; color: rgb(70, 85, 132); line-height: 16px; " ><?php <br> $dir = "files" ;<br><br> if ( is_dir ( $dir )) {<br> if ( $opendir = opendir( $dir )) {<br> while (( $file = readdir( $opendir )) !== false) {<br><br>unlink( $dir . "/" . $file );<br>}<br> closedir ( $opendir );<br>}<br>}<br>?></span> |
ซึ่งพอกดลบรูปแล้ว ขึ้น Error ว่า
Warning: unlink(files/.) [function.unlink]: Permission denied in C:xampplitehtdocsPhotoUploadDeleteFiles.php on line 9
Warning: unlink(files/..) [function.unlink]: Permission denied in C:xampplitehtdocsPhotoUploadDeleteFiles.php on line 9
รบกวนด้วยนะคะ ขอบคุณล่วงหน้ามากๆๆคะ
----------------------------------------------------------------
1 | |


เดาจากคำถาม ส่วนโค้ดดูไม่ออกจริงๆ
บอกว่าต้องการลบไฟล์แค่ไฟล์รูปเดียว แต่กลับลบไปหมด
ปัญหาคงไม่อยู่กับไฟล์ upload.php
น่าจะอยู่ที่
1.ไฟล์ Deletefiles.php
2.การส่งค่ามาลบไฟล์
เท่่าที่แกะโค้ด Deletefiles.php ดู ได้
1 2 3 4 5 6 7 8 9 10 11 | <?php $dir = "files" ; if ( is_dir ( $dir )) { // ตรวจสอบว่าเป็นโฟลเดอร์หรือไม่ if ( $opendir = opendir( $dir )) { // ถ้าเป็น ให้เข้าไปอ่านโฟลเดอร์น้้น while (( $file = readdir( $opendir )) !== false) { // วนลูปดึงข้อมูลรายการไฟล์ unlink( $dir . "/" . $file ); // ลบไฟล์แต่ละรายการ } closedir ( $opendir ); } } ?> |
โค้ดด้านบน ดูจากการทำงานเป็นการสั่งให้ลบไฟล์ทั้งหมดในโฟลเดอร์ชื่อ files
คงจะไม่สามารถใช้ิได้ ถ้าต้องการลบเฉพาะไฟล์บางรูป
ปกติถ้าเราต้องการลบไฟล์ใดๆ ก็จะใช้คำสั่งแค่
1 2 3 | <?php unlink( $dir . "/" . $file ); ?> |
เช่นจะลบไฟล์ aaa.jpg ในโฟลเดอร์ images
1 2 3 | <?php unlink( "images/aaa.jpg" ); ?> |
หรือส่งผ่านตัวแปร
1 2 3 4 5 | <?php $dir = "images" ; $file = "aaa.jpg" ; unlink( $dir . "/" . $file ); ?> |
ตัวอย่างเราส่งไฟล์ สำหรับลบ อย่างง่าย
Deletefiles.php?file=aaa.jpg
1 2 3 4 5 6 | <?php if ( $_GET [file']){ $dir = "images" ; unlink( $dir . "/" . $_GET [ 'file' ]); } ?> |

ขอบคุงมากจร้า
