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