steviez Posted May 4, 2009 Share Posted May 4, 2009 Hi, On my site i need to delete multiple files at once, the files would be selected by a check box and then you would hit delete. How would i make php handle this? Steve Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Loop through the check boxes checked and use unlink on whatever file it should remove. Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826331 Share on other sites More sharing options...
steviez Posted May 4, 2009 Author Share Posted May 4, 2009 how would i do the looping? im quite new to php Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826335 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Well give your check boxes an array name like 'remove[]' Then when the form is submitted, use $_POST['remove'] to get all the values of all check boxes selected. Then loop through the $_POST['remove'] array. If you're having trouble already, learn PHP first. Best advice I can give. Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826338 Share on other sites More sharing options...
steviez Posted May 5, 2009 Author Share Posted May 5, 2009 Well give your check boxes an array name like 'remove[]' Then when the form is submitted, use $_POST['remove'] to get all the values of all check boxes selected. Then loop through the $_POST['remove'] array. If you're having trouble already, learn PHP first. Best advice I can give. thanks for that, im currently trying to learn php thats why i need help. I have done what you said and when i use the print_r function i get this: Array ( [0] => on [1] => on [2] => on [3] => on [4] => on [5] => on ) how do i make it so i can delete my files from that? thanks Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826348 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Woah.. can you post the HTML form? Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826350 Share on other sites More sharing options...
steviez Posted May 5, 2009 Author Share Posted May 5, 2009 Woah.. can you post the HTML form? I have fixed that part now, its the while im stuck on... does it go like this? while($_POST['delete']) { // do stuff here } steve Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826353 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 No, that would be an infinite loop. Ever used foreach? Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826360 Share on other sites More sharing options...
steviez Posted May 5, 2009 Author Share Posted May 5, 2009 No, that would be an infinite loop. Ever used foreach? something like this? $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826378 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Yup, but you don't need the reference next to $value. Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826379 Share on other sites More sharing options...
steviez Posted May 5, 2009 Author Share Posted May 5, 2009 Yup, but you don't need the reference next to $value. this is my code so far and it wont work: <?php ## Delete multiple file ## if(isset($_POST['file_option']) && $_POST['file_option'] == 'delete') { // Get file path and file id $path = $setting['upload_path']; // Loop through data and delete files $files_array = array($_POST['delete']); foreach ($files_array as $files) { // Query database to get file info $query_file = @mysql_query("SELECT * FROM uploads WHERE id = '".$files."' AND upload_owner = '".$_SESSION['userid']."' LIMIT 1"); $file = @mysql_fetch_array($query_file); // Delete file from server @unlink($path.$file['image_name']); // Delete thumb from server @unlink($path.$file['image_thumb']); // Delete file from database @mysql_query("DELETE FROM uploads WHERE id = '".$files."' AND upload_owner = '".$_SESSION['userid']."' LIMIT 1"); // Redirect @header("Location:" . $url->url_base . '/myfiles'); } } ?> any ideas? Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826389 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 What's the HTML form? Also debug stuff, can you echo $path? Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826394 Share on other sites More sharing options...
steviez Posted May 5, 2009 Author Share Posted May 5, 2009 What's the HTML form? Also debug stuff, can you echo $path? <div class="right_side" id="right_side"> <form method="post" action="{$url->url_base}/myfiles" name="file_options"> <div class="file_options"> <select name="file_option"> <option value="" selected="selected">-- Selected Image Options --</option> <option value="delete">Delete image(s)</option> <option value="">-- Move Image To --</option> <option value="1">folder one</option> </select> <input type="submit" value="Go" /> <input type="button" value="Upload" /> <div style="margin-top:8px;"><strong>Select:</strong> <a href="javascript:;" onclick="javascript:selectToggle(true, 'file_options');">All</a>, <a href="javascript:;" onclick="javascript:selectToggle(false, 'file_options');">None</a></div> </div> {if $num_results == 0} <div align="center" style="margin-top:10px;"> <div class="info"> You currently have no uploaded images. </div> </div> {else} <style type="text/css">{literal} input.cb {position:absolute; margin-top:4px;} </style>{/literal} <div style="margin-top:10px;"> <table border="0" cellpadding="6" cellspacing="6"> <tbody> <tr> {foreach name=images item=row from=$results} <td> <div class="img" id="img_{$row.id}" style="text-align:center;margin:0 auto;"> <input type="checkbox" class="cb" name="delete[]" value="{$row.id}" /> <a href="{$url->url_base}/viewer/{$row.id}/{$row.image_name}"> <img src="./uploads/{$row.image_thumb}" alt="" class="image_thumb" id="{$row.id}" /> </a> </div> <div style="clear:both; text-align:left"></div> </td> {if $smarty.foreach.images.iteration mod $columns eq 0} </tr> {/if} {/foreach} </tr> </tbody> </table> </td> </tr> </table> </div> </form> {/if} </div> Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826395 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Your form doesn't even have name attribute for delete. Exactly, how does your code supposed to work. I have no idea what that HTML form is doing with that PHP code. They're like 2 different things. Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826398 Share on other sites More sharing options...
steviez Posted May 5, 2009 Author Share Posted May 5, 2009 Your form doesn't even have name attribute for delete. Exactly, how does your code supposed to work. I have no idea what that HTML form is doing with that PHP code. They're like 2 different things. fixed it! the form was 100% fine it was the following php code: $files_array = array($_POST['delete_file']); should be $files_array = $_POST['delete_file']; all fixed now thanks for your help. Link to comment https://forums.phpfreaks.com/topic/156868-solved-delete-multiple-files/#findComment-826406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.