{"id":4625,"date":"2024-05-09T11:13:55","date_gmt":"2024-05-09T10:13:55","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4625"},"modified":"2024-05-09T11:13:56","modified_gmt":"2024-05-09T10:13:56","slug":"file-download-from-a-string","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/html\/files-html\/file-download-from-a-string","title":{"rendered":"File download from a string"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Cause browser to prompt user to save file you will create from a string<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">HTML<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;form action=\"\/download-my-file\" target=\"_blank\" method=\"POST\">\n\n    &lt;select name=\"MyDownloadFileId\" style=\"width: 500px;\" onchange=\"this.form.submit()\" >\n      &lt;option value=\"0\">Select...&lt;\/option>\n      &lt;option value=\"1\">My file 1&lt;\/option>\n      &lt;option value=\"2\">My file 2&lt;\/option>\n    &lt;\/select>\n\n  &lt;\/form><\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">PHP<\/h5>\n\n\n\n<p>Basic example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>          header('Content-Disposition: attachment; filename=\"' . $FileName . '\"');\n          header('Content-Type: text\/txt');\n          header('Content-Length: ' . $ContentLength);\n          header('Connection: close');\n\n          echo ($FileContent);\n          die();<\/code><\/pre>\n\n\n\n<p>An example using WordPress:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'wp', 'mysite_wp' );\nfunction mysite_wp()\n{\n\n  if (strpos($_SERVER&#91;\"REQUEST_URI\"], '\/download-my-file') !== false)\n  {\n    if (isset($_REQUEST&#91;'MyDownloadFileId']))\n    {\n      \/\/---------------------------\n      \/\/----- DOWNLOAD A FILE -----\n      \/\/---------------------------\n      if (!current_user_can('administrator'))\n        die();\n      \n      $FileId = intval($_REQUEST&#91;'MyDownloadFileId']);\n      if ($FileId != 0)\n      {\n        $Result = db_files_read_file_content($FileId);\n        if ($Result !== False)\n        {\n          $FileName = $Result&#91;'FileName'];\n          $FileContent = $Result&#91;'FileContent'];\n          \n          $ContentLength = strlen($FileContent);\n          \n          header('Content-Disposition: attachment; filename=\"' . $FileName . '\"');\n          header('Content-Type: text\/txt');\n          header('Content-Length: ' . $ContentLength);\n          header('Connection: close');\n\n          echo ($FileContent);\n          die();\n        }\n      }\n    }\n  }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cause browser to prompt user to save file you will create from a string HTML PHP Basic example: An example using WordPress:<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[178,111],"tags":[],"class_list":["post-4625","post","type-post","status-publish","format-standard","hentry","category-files-wordpress","category-files-html"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4625"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4625\/revisions"}],"predecessor-version":[{"id":4626,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4625\/revisions\/4626"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}