1

I made an image uploader in WordPress using the

wp_upload_bits()

function. Here is my code how I do it

 $upload = wp_upload_bits($_FILES['portfolio_img']['name'][$i], null, file_get_contents($_FILES['portfolio_img']['tmp_name'][$i]));
   if(isset($upload['error']) != false ) {
   $portfolio_imgs[] = $upload['url'];
   add_post_meta($id, 'portfolio_imgs', $portfolio_imgs);
   update_post_meta($id, 'portfolio_imgs', $portfolio_imgs);
 }

I would like to allow the administrator to delete the uploaded images in the back end. I made the option and it deletes the meta infos, I can also get the images absolute url, for example:

http://localhost/wordpress/wp-content/uploads/2016/08/road.jpg

However I can't use the the

unlink()

PHP function for absolute URLs. I tried to convert the URLs to relative path but I can't make it work even with if I get a relative path like:

/wp-content/uploads/2016/08/road.jpg

or

wp-content/uploads/2016/08/road.jpg

I always get this error:

Warning: unlink(/wp-content/uploads/2016/08/road.jpg) [function.unlink]: No such file or directory in /home/...

I am really stuck with this any help is appriciated!

2
  • How do you get URL? Commented Aug 8, 2016 at 14:42
  • I have an array of each URL and I have a for loop to read each value 1 by 1 and do something with it 'for ($d=$delete_length-1; $d > -1; $d--) { if($selected[$d] == "delete") { unlink($current_images[$d]);} }'
    – István
    Commented Aug 8, 2016 at 14:59

1 Answer 1

1

Within your code snippet $upload['file'] will contain the full file path to the file. You could save this in the meta so that it's retrievable whenever you want to delete the file.

9
  • I have the absolute path of the file but I don't know how to delete it
    – István
    Commented Aug 8, 2016 at 14:57
  • Ah - that's not what your question says. The absolute path will be from your server's filesystem root, not your website's root. Commented Aug 8, 2016 at 15:00
  • Oh - do you have any idea how could I convert this URL: link to something I can use in unlink() function to delete the file?
    – István
    Commented Aug 8, 2016 at 15:04
  • Don't try and convert from the URL, just save $upload['file'] for later. Commented Aug 8, 2016 at 15:06
  • $upload['file'] has the absolute path of the file wp_die($upload['file']); returns: D:\work2016\xampp\htdocs\wordpress/wp-content/uploads/2016/08/road.jpg How do I delete the file using this absolute path?
    – István
    Commented Aug 8, 2016 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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