1

I am allowing user's to upload their images, so to show their uploaded images i am using<?php echo $options_images['image_bg'];?> which outputs this:http://www.mydomain.com/myfolder/wp-content/uploads/2013/05/deleteme.png. So now my image is displayed properly. Now i want to allow my users to delete that image and this is the where I failed miserably.

The problem: In order to delete that image I know I must use unlink() function but to actually use it i need to retrieve dynamically its directory only ('wp-content/uploads/2013/05/deleteme.png';) part (Correct me if I am wrong).

So somehow (I think at least) i need to read $options_images['image_bg'], and then get rid of http://www.mydomain.com/

but that path also can be anything
for example:http://www.mydomain.com/wp-content/uploads or http://www.mydomain.com/somefolder/someotherfolder/wp-content/uploads so I need to retrieve dynamically only somefolder/wp-content/uploads/2013/05/deleteme.png part but I have no idea how to achieve that! Currently I can delete my image but only when i am using my path hard coded like this:

<!--allow users to delete image
HARDCODED so it is a no go:(
-->      
<?php  $filename = 'wp-content/uploads/2013/05/deleteme.png';
if(file_exists('wp-content/uploads/2013/05/deleteme.png')){
unlink('wp-content/uploads/2013/05/deleteme.png');
}   
?>

I tried this also but it is not working

<?php  $filename = $options_images['image_bg'];
if(file_exists($filename)){
unlink($filename);
}   
?>

I am using this function inside my Theme options for image upload:

//Validate our settings here
function validate_setting($plugin_options){ 

//echo '<pre>';  print_r($_FILES); echo '</pre>';
$keys = array_keys($_FILES);
$i = 0;
foreach ($_FILES as $image) {        
    //If file was uploaded...
if($image['size']){
    //Is it an image?
    if(preg_match('/(jpg|jpeg|png|gif)$/i',$image['type'])){
        $override = array('test_form' => false);
      $file = wp_handle_upload($image,$override);         
      $plugin_options[$keys[$i]] = $file['url'];
    }
   else{
   wp_die('No image found');
   }
}
else{
 $options = get_option('sandbox_theme_social_options');
 $plugin_options[$keys[$i]] = $options[$keys[$i]];
}
$i++;
    } 
    return $plugin_options;
    }

But obviously this is a no go because image names can be anything and http://www.mydomain.com/can be changed to anything (http://www.mydomain.com/folder/other_folder/and_other_folder)

Can anybody help me with this?

0

Use wp_delete_attachment

<?php wp_delete_attachment( $attachment_id ); ?>
  • Can you please instruct me how to apply it in my example if it is an not a problem? Still learning PHP... THX!! – Dejo Dekic May 29 '13 at 16:45
  • What is this $options_images array? How is it generated? What you need to do is determine the ID of the attachment you want to delete, then pass it to the wp_delete_attachment function. Ideally you are getting an attachment somehow by a reference other than simply an absolute path to it. Otherwise it is tricky to get the ID, but not impossible. Let me know. – GhostToast May 29 '13 at 16:48
  • The problem is than I am not using Wordpress Media Uploader but custom function inside my Theme Options Page (edited my question to show you guy's how that function works... ) – Dejo Dekic May 29 '13 at 17:01
  • This is part of the problem with premium themes. They tend to step outside the core WordPress functions and do strange things, making it hard to do things "the WordPress way". Take a look at this guy's solution to looking up an attachment ID given a URL: pippinsplugins.com/retrieve-attachment-id-from-image-url – GhostToast May 29 '13 at 17:10
  • If this is your own theme or you feel comfortable editing it, you might wish to store the ID of the image and then whenever it is referenced, deal with its conversion from ID into image at that moment. Rather than storing an absolute URL. It would be cleaner in the long run, in my opinion. – GhostToast May 29 '13 at 17:29
0

Because you guy's helped me a lot with your answers this is how I did it if someone is interested:

<!--allow users to delete image-->
   <?php  $filename = $options_images['image_bg'];//Just storing my path to image here 
$urlparts = parse_url($filename );//I am getting rid of "http://www.mydomain.com" part here

   $extracted = $urlparts['path'];//Extracting my image path here 
   //So img path looks like this: /somefolder/wp-content/uploads/deleteme.png 
   //Only problem is dreaded trailing slash in front (this guy->/somefolder/)
   echo ltrim($extracted, '/');//We are getting rid of it via ltrim
   //Now image path looks like it should: somefolder/wp-content/uploads/deleteme.png 
   if(file_exists($extracted)){ //Now we can delete the images...
   unlink($extracted);
   }    
   ?><!--/allow users to delete image-->

Obviously i will need to check if the user chooses to delete image or not but it shouldn't be a problem. THX guys!!

0

I was going through the same problem as you are now.

Then I learned to convert url to path and delete any image or file from server. I am using this function for converting url to path and working fine for me.

 function url_to_path_test($url){
  $url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $url);
 return $url;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.