Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/63288-delete-image/
Share on other sites

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);

 

 

    }

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315423
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/63288-delete-image/#findComment-315507
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.