behzad Posted August 4, 2007 Share Posted August 4, 2007 Hi There Everyone. I need some help if someone be kind. I have a folder on the server which contains images. I like to delete the images which only thier name start with "wc_106" the file name might look like ( wc_106_xxxxxx.jpg , wc_106_yyyyy.jpg , wv_dddd.jpg ), but I only want to delete the ones having a prefix "wc_106". Could some one help me please? Quote Link to comment https://forums.phpfreaks.com/topic/63288-delete-image/ Share on other sites More sharing options...
Caesar Posted August 4, 2007 Share Posted August 4, 2007 Use regex, my friend. And then loop through your files and delete the ones that match the regular expression. Quote Link to comment https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315419 Share on other sites More sharing options...
behzad Posted August 4, 2007 Author Share Posted August 4, 2007 Hi There, Sorry I am new to this php, could you please help me a bit more please. Sorry to be a pain mate. Quote Link to comment https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315420 Share on other sites More sharing options...
cyber_ghost Posted August 4, 2007 Share Posted August 4, 2007 friend.. do worry.. every problme has its solution.. but it does not mean.. this is the absolute solution for your problem........ first: try to read the directory with php function.. using this codes: $mydir = dir('uploaded_documents/required_doc_file_listing/SF-07112007024747-RAG-01122007193937-0000006'); $mydir > is the directory of your image files.. then... while(($file = $mydir->read()) !== false) { echo "Filename: $file<BR>"; } >> which loops.. until all files from directory return to false... and try to read the $file: which this is filename of loop files.... then try to read $file using substr(); .. $sub_string = substr($file,5); // returns wc_106 if($sub_string=='wc_106') unlink($file); this is the complete code: $mydir = dir('uploaded_documents/required_doc_file_listing/SF-07112007024747-RAG-01122007193937-0000006'); while(($file = $mydir->read()) !== false) { $sub_string = substr($file,5); // returns wc_106 if($sub_string=='wc_106') unlink($file); } Quote Link to comment https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315423 Share on other sites More sharing options...
behzad Posted August 4, 2007 Author Share Posted August 4, 2007 Thanks for that, I am going to go over it and again thanks so much mate. Quote Link to comment https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315424 Share on other sites More sharing options...
cyber_ghost Posted August 4, 2007 Share Posted August 4, 2007 no problem.... and your welcome always... Quote Link to comment https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315426 Share on other sites More sharing options...
behzad Posted August 4, 2007 Author Share Posted August 4, 2007 Thanks Mate at start it did not work, I put a zero "0" and a comma in " $sub_string = substr($file,0,5); // returns wc_106 " and it is picking the ones up that has "wc_106" $mydir = dir('uploaded_documents/required_doc_file_listing/SF-07112007024747-RAG-01122007193937-0000006'); while(($file = $mydir->read()) !== false) { $sub_string = substr($file,0,5); // returns wc_106 if($sub_string=='wc_106') unlink($file); } ================================================ Now I am going to make the length of the designated charector to a variable and also the file name prefix $prefix = $_GET['swcl_catcode']; $length = strlen($prefix); $mydir = dir('../workout_images'); while(($file = $mydir->read()) !== false) { $sub_string = substr($file,0,"$length"); if($sub_string =="$prefix") unlink($file); } ================================================= The only thing is as usual the relative directory problem, I put like dir ('../wc') it did not work, I had to put it in the same directory as the image files. Well it is working now like this. Again thank you very much for the big help. Quote Link to comment https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315507 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.