允许用户在后台删除上传的图片?

时间:2013-05-29 作者:Dejo Dekic

我允许用户上传他们的图像,以便显示我使用的上传图像<?php echo $options_images[\'image_bg\'];?> 其输出:http://www.mydomain.com/myfolder/wp-content/uploads/2013/05/deleteme.png. 现在我的图像显示正确了。现在我想让我的用户删除这张图片,这就是我失败的地方

问题:为了删除该图像,我知道我必须使用unlink()但要实际使用它,我只需要动态检索它的目录(\'wp-content/uploads/2013/05/deleteme.png\';) 部分(如果我错了,请纠正我)
所以不知何故,我需要阅读$options_images[\'image_bg\'], 然后摆脱http://www.mydomain.com/http://www.mydomain.com/wp-content/uploads 或http://www.mydomain.com/somefolder/someotherfolder/wp-content/uploads 所以我只需要动态检索somefolder/wp-content/uploads/2013/05/deleteme.png 部分,但我不知道如何做到这一点 目前,我可以删除我的图像,但仅当我使用硬编码路径时,如下所示:

<!--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\');
}   
?>
我也试过了,但不起作用

<?php  $filename = $options_images[\'image_bg\'];
if(file_exists($filename)){
unlink($filename);
}   
?>
我正在我的主题选项中使用此功能进行图像上载:

//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;
    }
但显然这是不可能的,因为图像名称可以是任何http://www.mydomain.com/可以更改为任何内容(http://www.mydomain.com/folder/other_folder/and_other_folder)

有人能帮我吗

3 个回复
最合适的回答,由SO网友:Dejo Dekic 整理而成

因为你们在回答问题上帮了我很多,如果有人感兴趣,我就是这样做的:

<!--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-->
显然,我需要检查用户是否选择删除图像,但这应该不是问题。THX伙计们!!

SO网友:GhostToast

Use wp_delete_attachment

<?php wp_delete_attachment( $attachment_id ); ?>
SO网友:Vikas Bhardwaj

我正经历着和你现在一样的问题。

然后,我学会了将url转换为路径,并从服务器上删除任何图像或文件。我正在使用此函数将url转换为路径,并且工作正常。

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

结束