Why i am not able to delete the files(PHP)?

dododo

New Member
I use a form where i have listed the data from database like title, date etc. from the database and using checkboxes i use the multiple delete operationFor example my code look like this \[code\]<form method="post" action="action.php"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="http://stackoverflow.com/questions/3573966/<?php echo $row['id']; ?>"/><input name="delete" type="submit" id="delete" value="http://stackoverflow.com/questions/3573966/Delete"/>\[/code\]and in the action.php the code is like this\[code\] $checkbox = $_POST['checkbox']; //count the number of selected checkboxes in an array $count = count($checkbox); //Create a for loop to delete for($i=0;$i<$count;$i++) { $del_id = $checkbox[$i]; $sql = "DELETE FROM news WHERE id='$del_id'"; $result_delete_data = http://stackoverflow.com/questions/3573966/mysql_query($sql); }\[/code\]Now the table i want to delete actually have 5 table entities like title, timestamp,pic_title,pic_brief,pic_detail the last three entities i.e pic_title, pic_brief and pic_detail is actually storing the path of the image for example the value stored in one of the 3 entity would look like this \[code\]upload/file/pic_title1.jpg\[/code\] My problem is when i run my first for loop it successfully deletes the table without any problem but the file which exist in the file directory remains intact. i want to delete that file too, to remove that file i thought of adding another for loop which i did something like this\[code\]for($j=0;$j<$count;$j++){ $delete_id = $checkbox[$j]; $query = "SELECT news.pic_title, news.pic_brief, news.pic_detail FROM news WHERE id = '$delete_id'"; $result = mysql_query($query); $row = mysql_fetch_array($result); unlink($row['pic_title']); unlink($row['pic_brief']); unlink($row['pic_detail']); }\[/code\]The above code is unable to delete the requested file, my Query string is perfectly working fine i tested it by removing the unlink function and printing the values, it prints all the selected value , but it is refusing to delete the file and when i try to run the loop it shows error in the last three line, while i am pretty sure that, $row['pic_title'], $row['pic_brief'], $row['pic_brief'], have the full path of the image.\[code\] unlink($row['pic_title']); unlink($row['pic_brief']); unlink($row['pic_brief']);\[/code\]where i am going wrong?P.S: There is nothing wrong with file permission because when i individually try to run the function unlink it deletes the file from the same directory.EDIT : This is the error message i get\[code\]Warning: unlink() [function.unlink]: No error in C:\wamp\www\bn\admin-login\action.php on line 580Warning: unlink() [function.unlink]: No error in C:\wamp\www\bn\admin-login\action.php on line 581Warning: unlink() [function.unlink]: No error in C:\wamp\www\bn\admin-login\action.php on line 582\[/code\]To be more precise i tested this function individually in php and it is working perfectly fine\[code\]$target = 'upload/file/file1.jpg';unlink($target);\[/code\]and for this reason i dont think the file permission is causing the error, i guess i am going wrong somewhere with the logic.@Lekensteyn got me the solution, thank you Lekensteyn. actually i had to first hold the value in a variable and then unlink the file. the working code looks like this.\[code\] for($j=0;$j<$count;$j++){ $delete_id = $checkbox[$j]; $query = "SELECT * FROM news WHERE id = '$delete_id'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $pic_title = $row['pic_title']; $pic_brief = $row['pic_brief']; $pic_detail = $row['pic_detail']; unlink($pic_title); unlink($pic_brief); unlink($pic_detail); }\[/code\]
 
Back
Top