{"id":40,"date":"2013-11-17T01:36:49","date_gmt":"2013-11-17T01:38:55","guid":{"rendered":"https:\/\/e4315Downloads.jpg"},"modified":"2018-04-18T21:38:09","modified_gmt":"2018-04-19T03:38:09","slug":"descarga-de-archivos-con-php","status":"publish","type":"post","link":"https:\/\/emanuelpaxtian.com\/blog\/descarga-de-archivos-con-php\/","title":{"rendered":"Descarga de archivos con PHP"},"content":{"rendered":"<p>En esta ocasi&oacute;n les explico como realizar a la fuerza la descarga de un archivo, ya que en algunas ocasiones deseamos descargar determinado archivo y este se visualiza directamente en el navegador.<\/p>\n<p>La manera mas f&aacute;cil seria esta:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$file = $_GET&#x5B;'file'];\r\nheader(&quot;Content-disposition: attachment; filename=$file&quot;);\r\nheader(&quot;Content-type: application\/octet-stream&quot;);\r\nreadfile($file);\r\n<\/pre>\n<p>Pero este m&eacute;todo no es muy seguro, porque podrian descargar nuestros archivos del servidor como el index.php, ademas tendremos que verificar si el archivo existe o no,&nbsp; el modo seguro seria el siguiente:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif (!isset($_GET&#x5B;'file']) || empty($_GET&#x5B;'file'])) {\r\n exit();\r\n}\r\n$root = &quot;images\/&quot;;\r\n$file = basename($_GET&#x5B;'file']);\r\n$path = $root.$file;\r\n$type = '';\r\n\r\nif (is_file($path)) {\r\n $size = filesize($path);\r\n if (function_exists('mime_content_type')) {\r\n $type = mime_content_type($path);\r\n } else if (function_exists('finfo_file')) {\r\n $info = finfo_open(FILEINFO_MIME);\r\n $type = finfo_file($info, $path);\r\n finfo_close($info);\r\n }\r\n if ($type == '') {\r\n $type = &quot;application\/force-download&quot;;\r\n }\r\n \/\/ Definir headers\r\n header(&quot;Content-Type: $type&quot;);\r\n header(&quot;Content-Disposition: attachment; filename=$file&quot;);\r\n header(&quot;Content-Transfer-Encoding: binary&quot;);\r\n header(&quot;Content-Length: &quot; . $size);\r\n \/\/ Descargar archivo\r\n readfile($path);\r\n} else {\r\n die(&quot;El archivo no existe.&quot;);\r\n}<\/pre>\n<p>Ahora solo colocamos nuestro enlace de descarga.<\/p>\n<p><span><em>&lt;a href=&rdquo;download.php?file=imagen.jpg&rdquo;&gt;Descargar&lt;\/a&gt;<\/em><\/span><\/p>\n<p>y eso es todo , espero que le sirva.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En esta ocasi&oacute;n les explico como realizar a la fuerza la descarga de un archivo, ya que en algunas ocasiones deseamos descargar determinado archivo y este se visualiza directamente en&hellip;<\/p>\n","protected":false},"author":1,"featured_media":692,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":1,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":1098,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/1098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/media\/692"}],"wp:attachment":[{"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emanuelpaxtian.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}