Skip to main content
added 143 characters in body
Source Link
István
  • 85
  • 1
  • 11

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!

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 am really stuck with this any help is appriciated!

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!

Source Link
István
  • 85
  • 1
  • 11

Delete files uploaded using the wp_upload_bits() function

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 am really stuck with this any help is appriciated!