rxbanditboy1112 Posted January 19, 2007 Share Posted January 19, 2007 Hi,I have been trying to use php to remove files from my server. I have created a photo gallery upload system. Now i am trying to remove multiple photos at a time using a checkbox.I believe i need to use an array to store id numbers of things that are checked. Also not only do i want to remove the entries in mysql but i want to delete the actual photos also. I have been trying to use the unlink() function to delete files, but it gives me an error and tells me that the function does not exist.Something like that...Am i missing something? Is there a better way to do this?Thanks!!! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 Yes, you need to use an array.That's odd that unlink() does not work. Can you post the code and error? Quote Link to comment Share on other sites More sharing options...
rxbanditboy1112 Posted January 19, 2007 Author Share Posted January 19, 2007 Well now it doesn't give me an error, but it doesn't remove the database entry or the picture.case "perform":$sql = mysql_query("SELECT * FROM gallery_photos WHERE photo_category = $_POST[category_id]");$checked=$_POST[photos_tobedeleted];for ($i=0; $i < $_POST[number_of_rows]; $i++){ if ($checked[$i]) { $deleting_file_name = mysql_result(mysql_query("SELECT photo_filename FROM gallery_photos WHERE photo_id='$checked[$i]'"),0); unlink($_SERVER['DOCUMENT_ROOT'].'/fileupload/".$deleteing_file_name."'); $sql2 = mysql_query("DELETE FROM gallery_photos WHERE photo_id='$checked[$i]'"); if(!$sql2){ echo 'Error performing DELETE query: '. mysql_error(); } }}photos_tobedeleted are the name of the check boxes (but im not sure if i did that right). Quote Link to comment Share on other sites More sharing options...
rxbanditboy1112 Posted January 19, 2007 Author Share Posted January 19, 2007 Opps, ignore the first sql statement Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 This is going to cause a problem:unlink($_SERVER['DOCUMENT_ROOT'].'/fileupload/".$deleteing_file_name."');I think you want to do:unlink($_SERVER['DOCUMENT_ROOT'].'/fileupload/'.$deleteing_file_name); Quote Link to comment Share on other sites More sharing options...
rxbanditboy1112 Posted January 19, 2007 Author Share Posted January 19, 2007 Also i think im setting the checkboxes up incorrectly...<input type="checkbox" name="photos_tobedeleted[]" value="<? echo $photo_id;?>" />for every row it'll display that. Should it instead be photos_tobedelete[$i] ? Quote Link to comment Share on other sites More sharing options...
printf Posted January 19, 2007 Share Posted January 19, 2007 You should be deleting everything in one query, why loop?printf Quote Link to comment Share on other sites More sharing options...
printf Posted January 19, 2007 Share Posted January 19, 2007 What I mean... (less resources, not looping)this...[code]$checked=$_POST[photos_tobedeleted];for ($i=0; $i < $_POST[number_of_rows]; $i++){ if ($checked[$i]) { $deleting_file_name = mysql_result(mysql_query("SELECT photo_filename FROM gallery_photos WHERE photo_id='$checked[$i]'"),0); unlink($_SERVER['DOCUMENT_ROOT'].'/fileupload/".$deleteing_file_name."'); $sql2 = mysql_query("DELETE FROM gallery_photos WHERE photo_id='$checked[$i]'"); if(!$sql2){ echo 'Error performing DELETE query: '. mysql_error(); } }}[/code]could be[code]if ( ! empty ( $_POST['photos_tobedeleted'] ) ){ $photos = array_diff ( array_map ( 'intval', $_POST['photos_tobedeleted'] ), array ( 0 ) ); if ( ! empty ( $photos ) ) { $query = mysql_query ( "SELECT photo_filename FROM gallery_photos WHERE photo_id IN('" . implode ( "', '", $photos ) . "');" ); if ( mysql_num_rows ( $query ) > 0 ) { While ( $r = mysql_fetch_assoc ( $query ) ) { @unlink ( $_SERVER['DOCUMENT_ROOT'] . '/fileupload/' . $r['photo_filename'] ); } mysql_query ( "DELETE FROM gallery_photos WHERE photo_id IN('" . implode ( "', '", $photos ) . "');" ); } }}[/code]printf Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 [quote author=rxbanditboy1112 link=topic=123173.msg508818#msg508818 date=1169241377]Also i think im setting the checkboxes up incorrectly...<input type="checkbox" name="photos_tobedeleted[]" value="<? echo $photo_id;?>" />for every row it'll display that. Should it instead be photos_tobedelete[$i] ?[/quote]AFAIK the way you already have it, is the correct way. Quote Link to comment Share on other sites More sharing options...
rxbanditboy1112 Posted January 19, 2007 Author Share Posted January 19, 2007 I got it to work the other way, but i like the method you take printf. I never even knew you could do that haha. Thanks alot i am sure i will use the technique now. Quote Link to comment Share on other sites More sharing options...
rxbanditboy1112 Posted January 19, 2007 Author Share Posted January 19, 2007 Thank you also jesirose you helped alot also :)! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.