{"id":30279,"date":"2014-05-13T10:04:44","date_gmt":"2014-05-13T15:04:44","guid":{"rendered":"http:\/\/webirix.com\/?p=30279"},"modified":"2014-05-13T10:04:45","modified_gmt":"2014-05-13T15:04:45","slug":"borrar-archivos-en-php","status":"publish","type":"post","link":"https:\/\/webirix.com\/en\/borrar-archivos-en-php\/","title":{"rendered":"Delete files in PHP"},"content":{"rendered":"<p style=\"color: #222222;\">On the web, it is very common to work with files that the user sends through forms. On many occasions, for our own web application to maintain the files that have been uploaded, it will be necessary\u00a0<strong>delete files from PHP<\/strong>.<\/p>\n<p style=\"color: #222222;\">The first thing we must do is make sure that our application has permissions to be able to delete that file. You can see here\u00a0<a style=\"color: #f6933c;\" title=\"Change file permissions in PHP\" href=\"http:\/\/webirix.com\/en\/cambiar-permisos-de-archivo-con-php\/\" target=\"_blank\">how to change the permissions of a file in PHP<\/a>.<\/p>\n<p style=\"color: #222222;\">Once the file in question has the necessary permissions, we can proceed to delete it from PHP. The function we will use is\u00a0<strong>PHP unlink<\/strong>. Let&#039;s see its use.<\/p>\n<div class=\"wp_syntax\" style=\"color: #222222;\">\n<table style=\"height: 245px;\">\n<tbody>\n<tr>\n<td class=\"code\">\n<pre class=\"php\"><span style=\"color: #339933;\">&lt;<\/span> ? php\r\n     <span style=\"font-style: italic; color: #666666;\">\/\/ File that we are going to delete<\/span>\r\n     <span style=\"color: #000088;\">$ file<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">&#039;myfile.jpg&#039;<\/span><span style=\"color: #339933;\">;<\/span>\r\n     <span style=\"font-style: italic; color: #666666;\">\/\/ We proceed to delete<\/span>\r\n     <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">(<\/span><span style=\"color: #339933;\">!<\/span><span style=\"color: #990000;\">unlink<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$ file<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n          <span style=\"font-style: italic; color: #666666;\">\/\/ Failed to delete.<\/span>\r\n          <span style=\"color: #b1b100;\">threw out<\/span> <span style=\"color: #0000ff;\">&#039;Failed to delete&#039;<\/span><span style=\"color: #339933;\">;<\/span>\r\n     <span style=\"color: #009900;\">}<\/span> <span style=\"color: #b1b100;\">else<\/span> <span style=\"color: #009900;\">{<\/span>\r\n          <span style=\"font-style: italic; color: #666666;\">\/\/ File deleted<\/span>\r\n          <span style=\"color: #b1b100;\">threw out<\/span> <span style=\"color: #0000ff;\">&#039;File deleted&#039;<\/span><span style=\"color: #339933;\">;<\/span>\r\n     <span style=\"color: #009900;\">}<\/span>\r\n? <span style=\"color: #339933;\">&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"color: #222222;\">With this simple script we can\u00a0<strong>delete files from PHP<\/strong>.<\/p>\n<p style=\"color: #222222;\">Have you done something similar? Leave us a comment<\/p>","protected":false},"excerpt":{"rendered":"<p>On the web it is very common to work with files that the user sends through forms. On many occasions, for our own web application to maintain the files that have been uploaded, it will be necessary to delete files from PHP. The first thing we must do is make sure that our application has permissions to be able to delete ...<\/p>","protected":false},"author":2,"featured_media":30277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2014],"tags":[],"class_list":["post-30279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programacionweb"],"jetpack_featured_media_url":"https:\/\/webirix.com\/wp-content\/uploads\/2014\/05\/elephpant.png","_links":{"self":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/30279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/comments?post=30279"}],"version-history":[{"count":1,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/30279\/revisions"}],"predecessor-version":[{"id":30280,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/30279\/revisions\/30280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media\/30277"}],"wp:attachment":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media?parent=30279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/categories?post=30279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/tags?post=30279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}