ต้องการที่จะลบรูปทีละภาพได้อ่าคะ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ต้องการที่จะลบรูปทีละภาพได้อ่าคะ

ต้องการที่จะลบรูปทีละภาพได้อ่าคะ

 ที่สร้างคือ 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


รบกวนด้วยนะคะ ขอบคุณล่วงหน้ามากๆๆคะ



Toonjunt 158.108.157.xxx 17-02-2010 22:26:42

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1

ง่ะ ขอโทดด้วยคะ ไหงข้อความมันรวมไปหมดเลยอ่า แก้ให้ใหม่นะคะ 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';  ?>
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


รบกวนด้วยนะคะ ขอบคุณล่วงหน้ามากๆๆคะ

----------------------------------------------------------------

 

 

 



toonjunt 158.108.157.xxx 17-02-2010 22:30
 ความคิดเห็นที่ 2

ง่ะ Code ไม่รู้เลย อัพเป้นไฟล์ .txt ให้นะคะ T_T

userfiles/Error.txt 



toonjunt 158.108.157.xxx 17-02-2010 22:35
 ความคิดเห็นที่ 3

เดาจากคำถาม ส่วนโค้ดดูไม่ออกจริงๆ
บอกว่าต้องการลบไฟล์แค่ไฟล์รูปเดียว แต่กลับลบไปหมด
ปัญหาคงไม่อยู่กับไฟล์ 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']);
}
?>

 



ninenik 124.121.245.xxx 18-02-2010
 ความคิดเห็นที่ 4

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



toonjunt 222.123.91.xxx 22-02-2010 04:30
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ